2017-10-03 7 views
1

私はこの配列を持っている:月のマルチ配列検索でループしない方法と月の名前を挿入するには?

$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)); 

この配列は、データベースから毎月のデータを数えます。

ただし、データが入っている月のみをリストします。データがないのに1月から12月までの別の月を表示したい。

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

どのように私は$行の配列にループしたいと配列には月がない場合見つけ、それは$ヶ月から月の名前を挿入します:

は、だから私は、数ヶ月のダミー配列を作成します内部に0の値がありますか?

私のサンプルデータ:

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] =>)))) 

予想される出力:

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] =>)))) 
+2

あなたのサンプルデータと予想される出力 –

答えて

0

あなたはこのようにそれを行うことができます。

注:私はあなたが与えている月とアレイのすべてのDBの値が同じであると仮定しています。

このコードでは、追加されたすべての月を収集し、残りの月のデータを追加しています。残りの月はarray_diffです。

Try this code snippet heresample 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); 
+1

はいを​​見ます!絶対に正しい答えです。 Brilliant @Sahil Gulati。あなたの答えはまったく正しいものであり、わかりやすいとは思いません。私はそれを行うのは複雑なコードだと思った。ありがとう!! – joun

+1

私はすでにあなたの答えを受け入れる...あなたの助けをよろしくお願いします – joun

+0

@joun歓迎の友人...あなたを助けてうれしい... :) –

関連する問題