我想组合两个数组,每个数组都有相同的索引(此处为ID),但我不想删除重复的ID (抱歉我的英文...)
First array name : $data['student']
Second array name: $data_student_name
我有这个数组($data['student']
)
Array ( [0] => Array ( [id] => 16 [groups_id] => 1 [group_number] => 301 ) [1] => Array ( [id] => 17 [groups_id] => 1 [group_number] => 301 ) [2] => Array ( [id] => 18 [groups_id] => 1 [group_number] => 301 ) [3] => Array ( [id] => 5 [groups_id] => 1 [group_number] => 301 ) [4] => Array ( [id] => 14 [groups_id] => 1 [group_number] => 301 ) [5] => Array ( [id] => 18 [groups_id] => 2 [group_number] => 434 ) [6] => Array ( [id] => 19 [groups_id] => 2 [group_number] => 434 ) [7] => Array ( [id] => 19 [groups_id] => 1 [group_number] => 301 ) )
和此数组$data_student_name
Array ( [0] => Array ( [id] => 5 [firstname] => Bruno [sex] => M ) [1] => Array ( [id] => 14 [firstname] => David [sex] => M ) [2] => Array ( [id] => 16 [firstname] => Élyse [sex] => F ) [3] => Array ( [id] => 17 [firstname] => Jean [sex] => M ) [4] => Array ( [id] => 18 [firstname] => Julie [sex] => F ) [5] => Array ( [id] => 19 [firstname] => Marie [sex] => F ) )
我将我的两个数组与此
组合在一起 $data['group_student']= array();
foreach ($data_student_name as $arr) {
$match = false;
foreach ($data['student'] as $arr2) {
if ($arr['id'] == $arr2['id']) {
$match = true;
$data['group_student'][] = array_merge($arr, $arr2);
break;
}
}
if ( !$match ) $data['group_student'][] = $arr;
}
我得到了这个结果(玛丽没有重复:()($data['group_student']
)
Array ( [0] => Array ( [id] => 5 [firstname] => Bruno [sex] => M [groups_id] => 1 [group_number] => 301 ) [1] => Array ( [id] => 14 [firstname] => David [sex] => M [groups_id] => 1 [group_number] => 301 ) [2] => Array ( [id] => 16 [firstname] => Élyse [sex] => F [groups_id] => 1 [group_number] => 301 ) [3] => Array ( [id] => 17 [firstname] => Jean [sex] => M [groups_id] => 1 [group_number] => 301 ) [4] => Array ( [id] => 18 [firstname] => Julie [sex] => F [groups_id] => 1 [group_number] => 301 ) [5] => Array ( [id] => 19 [firstname] => Marie [sex] => F [groups_id] => 2 [group_number] => 434 ) 1 ))
我想要这个结果(玛丽是重复的)
Array ( [0] => Array ( [id] => 5 [firstname] => Bruno [sex] => M [groups_id] => 1 [group_number] => 301 ) [1] => Array ( [id] => 14 [firstname] => David [sex] => M [groups_id] => 1 [group_number] => 301 ) [2] => Array ( [id] => 16 [firstname] => Élyse [sex] => F [groups_id] => 1 [group_number] => 301 ) [3] => Array ( [id] => 17 [firstname] => Jean [sex] => M [groups_id] => 1 [group_number] => 301 ) [4] => Array ( [id] => 18 [firstname] => Julie [sex] => F [groups_id] => 1 [group_number] => 301 ) [5] => Array ( [id] => 19 [firstname] => Marie [sex] => F [groups_id] => 2 [group_number] => 434 ) [6] => Array ( [id] => 19 [firstname] => Marie [sex] => F [groups_id] => 2 [group_number] => 301 ))
我该怎么办呢?
答案 0 :(得分:0)
答案 1 :(得分:0)
使用array_merge
array_merge($arr1, $arr2);