joun joun - 11 months ago 78
PHP Question

How to loop in multi array search for months not exist and insert the month name?

I have this array:

$rows = array_map(function($item) {
return (object) ['c' => [
(object) ['v' => $item->month, 'f' => null],
(object) ['v' => intval($item->jobcount), 'f' => null],
(object) ['v' => intval($item->interncount), 'f' => null]
]];
}, array_values($data));


This array count the data in every month from database.

However, it only list out the months with data. I want to display other months from January to December although it doesn't have data.

So I create a dummy array of months:

$months = array('January', 'February', 'March', 'April', 'May', 'Jun', 'July', 'August', 'September', 'October', 'November', 'December');


How I want to loop into the $rows array and find if there is no month in the array, it will insert the month name from $months with 0 values inside?

My sample data:

Array ( [0] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => July [f] => ) [1] => stdClass Object ( [v] => 6 [f] => ) [2] => stdClass Object ( [v] => 2 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => August [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 3 [f] => ) ) ) )


The expected output:

Array ( [0] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => January [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => February [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => March [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => April [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => Mei [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => Jun [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => July [f] => ) [1] => stdClass Object ( [v] => 6 [f] => ) [2] => stdClass Object ( [v] => 2 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => August [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 3 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => September [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => October [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => November [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => December [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) )

Answer Source

You can do it like this.

Note: I am assuming all the DB values for months and array which you have given are same.

In this code below we are gathering all the months which has been added and then adding data for remaining months. We are getting remaining months using array_diff.

Try this code snippet here sample demo data

$months = array('January', 'February', 'March', 'April', 'May', 'Jun', 'July', 'August', 'September', 'October', 'November', 'December');
$monthPicked=array();//months added to result

$rows = array_map(function($item) use(&$monthPicked){
    array_push($monthPicked,$item->month);//pushing added months
return (object) ['c' => [
    (object) ['v' => $item->month, 'f' => null],
    (object) ['v' => intval($item->jobcount), 'f' => null],
    (object) ['v' => intval($item->interncount), 'f' => null]
]];
}, array_values($data));

$remainingMonths=array_diff($months,$monthPicked);//remaining months

foreach($remainingMonths as $month)
{
    $rows[]=(object) ['c' => [
    (object) ['v' => $month, 'f' => null],
    (object) ['v' => 0, 'f' => null],
    (object) ['v' => 0, 'f' => null]
]];
}
print_r($rows);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download