This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
4 => array(
'title' => 'test1',
'count' => 40,
'month' => 'jun-2015'
),
);
I've to convert this array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
2 => array(
'title' => 'test3',
'count' => 0,
),
3 => array(
'title' => 'test4',
'count' => 0,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test1',
'count' => 40,
),
1 => array(
'title' => 'test2',
'count' => 0,
),
2 => array(
'title' => 'test3',
'count' => 14,
),
3 => array(
'title' => 'test4',
'count' => 0,
),
),
'july-2015' => array(
0 => array(
'title' => 'test1',
'count' => 0,
),
1 => array(
'title' => 'test2',
'count' => 0,
),
2 => array(
'title' => 'test3',
'count' => 0,
),
3 => array(
'title' => 'test4',
'count' => 45,
),
),
);
That is if there is no title for particular month, then i need to add that title and count will be 0. how to do this? I've tried this concept:
$gen_arr = array_fill_keys(array_column($arr, 'month'), array());
$final_arr = array(); // Array to store the result
foreach ($gen_arr as $gen_key => $gen_value) {
foreach ($arr as $org_key => $org_value) {
//temporarily store the original array
$temp = $org_value;
if ($gen_key != $temp['month'] && !in_array($temp['title'], $final_arr[$gen_key])) {
$temp['month'] = $gen_key;
$temp['count'] = 0;
}
$final_arr[$gen_key][] = $temp;
}
}
return $final_arr;
But i can't get what i wanted. Is any other solutions for this?