There are 2 arrays:

$m1= Array ( [0] => 123 [1] => 456 ) $m2= Array ( [0] => array ( [num]=123 [val]=fff ) [1] => array ( [num]=123 [val]=ccc ) [2] => array ( [num]=123 [val]=eee ) [3] => array ( [num]=456 [val]=asd ) [4] => array ( [num]=456 [val]=ghj ) ) 

Is it possible for some regular functions in PHP to get the result in the form of such an array, based on the previous two:

 $m3 = Array ( [0] => array ( [num]=123; [values]= fff, ccc, eee ) [1] => array ( [num]=456; [values]= asd, ghj ) ) 
  • one
    You go over the second array, merge the values ​​with the same numbers into the new array, and then apply the logic from the first one. ready-made magic does not happen, and if it does, callback methods will still have to be written. - teran

1 answer 1

No, the regular functions of this will not work. Here is the implementation:

 $m3 = []; foreach($m1 => $key as $num) { if (isset($m2[$key])) { // запишет только если соответствует ключу в массиве $m2 $m3[$key]['num'] = $num; $m3[$key]['values'] = $m2[$key]['val']; } }