$list = Array ( [Jul] => Array ( [deposit] => Array ( [totalcount] => 1 [totalamount] => 12 ) ) [Oct] => Array ( ) [Nov] => Array ( ) [Dec] => Array ( [deposit] => Array ( [totalcount] => 2 [totalamount] => 2400 ) [withdraw] => Array ( [totalcount] => 1 [totalamount] => 3000 ) ) )
//my code
foreach ($list as $ekey => $evalue) { if(!array_column($list[$ekey], 'deposit')){ $list[$ekey]['deposit'] = array( 'totalcount' => 0, 'totalamount' => 0 ); }else if(!array_column($list[$ekey], 'withdraw')){ $list[$ekey]['withdraw'] = array( 'totalcount' => 0, 'totalamount' => 0 ); } }
Question: Above code is to check whether the data inside each array have the same key, does it mean that every key in the array should have the “deposit” and “withdraw”. If not found it will assign the default value to it. But my code will only insert deposit value into those missing, it will not insert withdraw value into it? Anyone can help with this :(??
Advertisement
Answer
It would be easier just to merge the existing values into the defaults:
$default = array('deposit' => array('totalcount' => 0, 'totalamount' => 0), 'withdraw' => array('totalcount' => 0, 'totalamount' => 0)); foreach ($list as &$evalue) { $evalue = array_merge($default, $evalue); }