0

I have 3 arrays:

Array
(
    [0] => l_ka3a1
    [1] => l_ka3a2
    [2] => l_ka3a3
)
Array
(
    [0] => l_no_inspection
    [1] => l_elbab_alresi
    [2] => l_bab_alqa3a
)
Array
(
    [0] => notes
    [1] => notes
    [2] => null
)

I want to collect them in 1 array:

Array(
    Array
    (
        [0] => l_ka3a1
        [1] => l_no_inspection
        [2] => notes
    )
    Array
    (
        [0] => l_ka3a2
        [1] => l_elbab_alresi
        [2] => notes
    )
    Array
    (
        [0] => l_ka3a3
        [1] => l_bab_alqa3a
        [2] => null
    )
)

witch every index in old arrays with the same one in another and collect them in 1 array.

Mohammad
  • 21,175
  • 15
  • 55
  • 84

3 Answers3

1

Use array_map() to creating new array contain target three arrays.

$newArr = array_map(function($v1, $v2, $v3){
    return [$v, $v2, $v3];
}, $arr1, $arr2, $arr3);

Check result in demo

Mohammad
  • 21,175
  • 15
  • 55
  • 84
0

One of many ways using simple for loop,

$expected = [];
for($i=0;$i<3;$i++){
    $expected[] = [$arr1[$i],$arr2[$i],$arr3[$i]];
}
print_r($expected);

DEMO: https://3v4l.org/9Oo5D

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

I don't know which one do you like. But for me, I will do like this.

$array = [$arr1, $arr2, $arr3];

OR I will make it more dynamic using array_push()

  $array = [];
  array_push($array, $arr1);
  // Some code maybe
  array_push($array, $arr2);
  // Some code maybe
  array_push($array, $arr3);
  • 1
    Please pay attention to how array elements are combined. In your case you just put the 3 arrays to one larger array, but in the question arrays are joined in a different way. – Andriy B Nov 25 '18 at 17:47