'Create multidimensional array from two arrays and set unique id as key of main array

Think, I have two arrays as showing below:

$a1 = [1=>444,2=>555,3=>666,4=>777];
$a2 = [1=>888,2=>999,3=>444,4=>'',5=>123,6=>215];

Then, my new array should be :

Array
(
  [1] => Array
          (
            ['p1'] => 444
            ['p2'] => 888
          ),  
  [2] => Array
          (
            ['p1'] => 555
            ['p2'] => 999
          ),  
  [3] => Array
          (
            ['p1'] => 666
            ['p2'] => 444
          ),
  [4] => Array
          (
            ['p1'] => 777
            ['p2'] => 
          )
)

I tried its as shown the code below. Its working for me, But my I know is there any other appropriate way to do this without looping in php?

foreach ($a1 as $k => $v) {
    $nary[$k] = ['p1' => $v, 'p2'=>$a2[$k]];
}


Solution 1:[1]

There is no reason to seek an alternative syntax. No matter what style you use, some sort of iteration/looping will be required.

Just use what you have. Functional-style iterators are likely to less concise than your foreach loop.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1