php - merge two array with the same key and value -


i have arrays of $array_one:

print_r($array_one); array (     [0] => stdclass object         (             [myid] => 653509             [date] => 2015-03-15 00:07:03         )      [1] => stdclass object         (             [myid] => 653511             [date] => never         )      [2] => stdclass object         (             [myid] => 653530             [date] => 2015-03-15 02:06:26         ) 

and arrays of $array_two;

print_r($array_two);  array (      [0] => stdclass object         (             [myid] => 653530             [pin] => 12fdg34345         )      [1] => stdclass object         (             [myid] => 653509             [pin] => 1we2534dgf5         )      [2] => stdclass object         (             [myid] => 653511             [pin] => 12wer3u45         ) 

and want merge based on keys same value, in expected result be:

array (      [0] => stdclass object         (             [myid] => 653530             [pin] => 12fdg34345             [date] => 2015-03-15 02:06:26         )      [1] => stdclass object         (             [myid] => 653509             [pin] => 1we2534dgf5             [date] => 2015-03-15 00:07:03         )      [2] => stdclass object         (             [myid] => 653511             [pin] => 12wer3u45             [date] => never         ) 

from result above, array key of date first_array push second_array based on similar value of key of my_id.

is there way this?

please , many help.

cheers!

as per comment, this solution depends on structure changing slightly, arrays of associative arrays, not arrays of objects. said data coming (a) database(s), if you're using pdo, should mean small change set correct fetch mode.

the result can achieved combining php built-in functions array_column , array_replace_recursive. if want resulting array still 0-indexed, can use array_values too.

$array_one = [     [         'myid' => 653509,         'date' => '2015-03-15 00:07:03'     ],     [         'myid' => 653511,         'date' => 'never'     ],     [         'myid' => 653530,         'date' => '2015-03-15 02:06:26'     ] ];  $array_two = [     [         'myid' => 653530,         'pin' => '12fdg34345'     ],     [         'myid' => 653509,         'pin' => '1we2534dgf5'     ],     [         'myid' => 653511,         'pin' => '12wer3u45'     ] ];  $merged = array_replace_recursive(     array_column($array_one, null, 'myid'),     array_column($array_two, null, 'myid') ); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -