Merge an array with an existing array php -
i have 2 array both containing data. want able add them information second array joins first array. array_merge doing adds second array end of first one.
array 1
[1] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 16.11 [4] => 80.56 [5] => 96.67 ) [2] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 1.23 [4] => 50.69 [5] => 14.24 )
array 2
[1] => array ( [0] => time [1] => lorem [2] => ipsum ) [2] => array ( [0] => time [1] => [2] => text )
how can merge 2 arrays output becomes below?
array 3
[1] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 16.11 [4] => 80.56 [5] => 96.67 [6] => time [7] => lorem [8] => ipsum ) [2] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 1.23 [4] => 50.69 [5] => 14.24 [6] => time [7] => [8] => text )
what happening
[1] => array ( [0] => 2017-07-14 00:00:00 [1] => foo [2] => bar [3] => 16.11 [4] => 80.56 [5] => 96.67 ) [2] => array ( [0] => 2017-07-14 00:00:00 [1] => foo [2] => bar [3] => 1.23 [4] => 50.69 [5] => 14.24 ) [3] => array ( [0] => time [1] => lorem [2] => ipsum ) [4] => array ( [0] => time [1] => [2] => text )
i have tried array_merge( $array1 , $array2 );
adds second array end of first one.
any ideas?
this looks pretty forward me, array_merge()
elements of 2 arrays:
<?php $a = [ 1 => [ '2017-07-13 00:00:00', 'foo', 'bar', 16.11, 80.56, 96.67 ], 2 => [ '2017-07-13 00:00:00', 'foo', 'bar', 1.23, 50.69, 14.24 ] ]; $b = [ 1 => [ 'time', 'lorem', 'ipsum' ], 2 => [ 'time', 'some', 'text' ] ]; array_walk($b, function($values, $key) use (&$a) { $a[$key] = array_merge($a[$key], $values); }); print_r($a);
the output of is:
array ( [1] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 16.11 [4] => 80.56 [5] => 96.67 [6] => time [7] => lorem [8] => ipsum ) [2] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 1.23 [4] => 50.69 [5] => 14.24 [6] => time [7] => [8] => text ) )
update:
in comment below ask if approach can generalized merge arbitrary number of arrays. sure possible, add iteration layer:
<?php $target = [ 1 => ['2017-07-13 00:00:00', 'foo', 'bar', 16.11, 80.56, 96.67], 2 => ['2017-07-13 00:00:00', 'foo', 'bar', 1.23, 50.69, 14.24] ]; $sources = [ 'b' => [ 1 => ['time', 'lorem', 'ipsum'], 2 => ['time', 'some', 'text'] ], 'c' => [ 1 => ['c1a', 'c1b'], 2 => ['c2a', 'c2b', 'c2b'] ] ]; array_walk($sources, function($source) use (&$target) { array_walk($source, function($values, $key) use (&$target) { $target[$key] = array_merge($target[$key], $values); }); }); print_r($target);
this variant produces output:
array ( [1] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 16.11 [4] => 80.56 [5] => 96.67 [6] => time [7] => lorem [8] => ipsum [9] => c1a [10] => c1b ) [2] => array ( [0] => 2017-07-13 00:00:00 [1] => foo [2] => bar [3] => 1.23 [4] => 50.69 [5] => 14.24 [6] => time [7] => [8] => text [9] => c2a [10] => c2b [11] => c2b ) )
Comments
Post a Comment