0

I would like to merge two arrays in a way that values of first array are transformed in first elements of the subarray and values of the second array are transformed in second elements of the subarray. Here my example:

Array01
(
    [0] => 41558194
    [1] => 44677841
    [2] => 44503689
    [3] => 40651770
)

Array02
(
    [0] => 551
    [1] => 546
    [2] => 531
    [3] => 519
)

mergedArray
(
    [0] => Array([0] => 41558194 [1] => 551)
    [1] => Array([0] => 44677841 [1] => 546)
    [2] => Array([0] => 44503689 [1] => 531)
    [3] => Array([0] => 40651770 [1] => 519)
)

What is the most efficient way to do this? Many thanks in advance!

Aloysia de Argenteuil
  • 833
  • 2
  • 11
  • 27

2 Answers2

1

Here is a concise example using array_map:

function merge_arrays($a1, $a2) {
    return array($a1, $a2);
}

$result = array_map("merge_arrays", $arr, $arr2);
starless13
  • 424
  • 2
  • 7
0

An example with your values :

  $array1 = array(
        0 => 41558194,
        1 => 44677841,
        2 => 44503689,
        3 => 40651770
    );

    $array2 = array(
        0 => 551,
        1 => 546,
        2 => 531,
        3 => 519
    );


    $finalArray = array();

    foreach ($array1 as $key1 => $value1) {
        $finalArray[$key1] = array($value1, $array2[$key1]);
    }
Alban Pommeret
  • 327
  • 1
  • 10