0

here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.

How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:

Array 01:

Array01 (

[0] => Array
    (
        [0] => 40292633
        [1] => 412
    )

[1] => Array
    (
        [0] => 41785603
        [1] => 382
    )

[2] => Array
    (
        [0] => 48792980
        [1] => 373
    )

[3] => Array
    (
        [0] => 44741143
        [1] => 329
    ))

Array 02:

Array02(

[0] => Array
    (
        [0] => 3460581
        [1] => 1407424B1
        [2] => 951753
    )

[1] => Array
    (
        [0] => 3484251
        [1] => 1028325B1
        [2] => 159357
    )

[2] => Array
    (
        [0] => 3519102
        [1] => 0586365A1
        [2] => 456654
    )

[3] => Array
    (
        [0] => 3529714
        [1] => 1059876A1
        [2] => 852258
    ))

Final array:

finalArray(

[0] => Array
    (
        [0] => 40292633
        [1] => 412
        [2] => 3460581
        [3] => 1407424B1
        [4] => 951753
    )

[1] => Array
    (
        [0] => 41785603
        [1] => 382
        [2] => 3484251
        [3] => 1028325B1
        [4] => 159357
    )

[2] => Array
    (
        [0] => 48792980
        [1] => 373
        [2] => 3519102
        [3] => 0586365A1
        [4] => 456654
    )

[3] => Array
    (
        [0] => 44741143
        [1] => 329
        [2] => 3529714
        [3] => 1059876A1
        [4] => 852258
    ))

Many thanks in advance!

Community
  • 1
  • 1
Aloysia de Argenteuil
  • 833
  • 2
  • 11
  • 27

2 Answers2

4

try this code

function merge_arrays($a1, $a2) {

        return array($a1, $a2);
}

$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
    $nres[] = array_merge ($nr[0], $nr[1]);
}
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
  • It's a clever solution that answers the question (I upvoted it), but notice that might not give expected results if the arrays have different keys, or the keys aren't integers starting with a zero or if the keys don't follow the same order. – Pedro Amaral Couto Oct 17 '14 at 14:15
0

Try this:

$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
    if( !array_key_exists($key, $arr1) ) {
        $result[$key] = $arr2;
    } else if( !array_key_exists($key, $arr2) ) {
        $result[$key] = $arr1;
    } else {
        $result[$key] = array_merge($arr1[$key], $arr2[$key]);
    }
}

It does not assume that both arrays have the same keys.

If you'd like to use array_map, this is an equivalent solution:

$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
                        if( !array_key_exists($key, $arr1) ) {
                            return $arr2;
                        } else if( !array_key_exists($key, $arr2) ) {
                            return $arr1;
                        }
                        return array_merge($arr1[$key], $arr2[$key]);
                    },
                    $keys);

If you're sure both arrays have the same keys, you can try this:

$result = array();
foreach($arr1 as $key => $arr1val) {
    $result[$key] = array_merge($arr1val, $arr2[$key]);
}
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15