3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = Array ( 0 => Array ( 'member_id' => 6, 'total' => 1, 'sorting' => 0 ), 1 => Array ( 'member_id' => 7, 'total' => 2, 'sorting' => 1 ) ); $b = Array ( 0 => Array ( 'member_id' => 2, 'total' => 3 ), 1 => Array ( 'member_id' => 6, 'total' => 1 ), 2 => Array ( 'member_id' => 7, 'total' => 2 ) ); $new_array = []; foreach ($a as $key => $data) { $new_array[$key]['member_id'] = $data['member_id']; foreach ($b as $key_1 => $data_1) { if ($data['member_id'] == $data_1['member_id']) { $new_array[$key]['total'] = $data_1['total']; if (isset($data_1['sorting'])) { $new_array[$key]['sorting'] = $data_1['sorting']; } else { $new_array[$key]['sorting'] = $data['sorting']; } break; } else { $new_array[$key]['total'] = 0; $new_array[$key]['sorting'] = $data['sorting']; } } } $new_array1 = []; foreach ($b as $key => $data) { if ($data['member_id'] != isset($new_array[$key]['member_id'])) { $new_array1[$key]['member_id'] = $data['member_id']; $new_array1[$key]['total'] = $data['total']; if (!isset($data['sorting'])) { $new_array1[$key]['sorting'] = isset($new_array[$key]['sorting']) ? $new_array[$key]['sorting'] + 1 : "" ; } } } $array_merge = array_merge($new_array, $new_array1); print_r($array_merge);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
Array ( [0] => Array ( [member_id] => 6 [total] => 1 [sorting] => 0 ) [1] => Array ( [member_id] => 7 [total] => 2 [sorting] => 1 ) [2] => Array ( [member_id] => 7 [total] => 2 [sorting] => ) )

preferences:
167.15 ms | 408 KiB | 5 Q