php - How can I merge one array with values into an array with stdClass objects? -
i have 2 arrays , looking way merge them. standard array_merge()
function don't work.
do know nice solution without foreach iteration?
my first array:
array ( [0] => stdclass object ( [field_value] => green [count] => ) [1] => stdclass object ( [field_value] => yellow [count] => ) )
my second array:
array ( [0] => 2 [1] => 7 )
and result get:*
array ( [0] => stdclass object ( [field_value] => green [count] => 2 ) [1] => stdclass object ( [field_value] => yellow [count] => 7 ) )
[akshay@localhost tmp]$ cat test.php <?php $first_array = array( (object)array("field_value"=>"green","count"=>null), (object)array("field_value"=>"yellow","count"=>null) ); $second_array = array(2,7); function simple_merge($arr1, $arr2) { return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2); } print_r($first_array); print_r($second_array); print_r(simple_merge($first_array,$second_array)); ?>
output
[akshay@localhost tmp]$ php test.php array ( [0] => stdclass object ( [field_value] => green [count] => ) [1] => stdclass object ( [field_value] => yellow [count] => ) ) array ( [0] => 2 [1] => 7 ) array ( [0] => stdclass object ( [field_value] => green [count] => 2 ) [1] => stdclass object ( [field_value] => yellow [count] => 7 ) )
Comments
Post a Comment