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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -