0

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]];
}
thara
  • 27
  • 1
  • 12

1 Answers1

0

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136