<?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);
preferences:
23.95 ms | 405 KiB | 5 Q