情境:
输入:指定阵列&教师规模
A1:
[designation] => Array
(
[0] => 24
[1] => 25
[2] => 26
[3] => 27
[4] => 24
[5] => 25
)
[grade_scale] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 1
[5] => 10
)
现在,有一个相同的名称在A1数组中重复两次,这很好,因为A2中不同等级的相同名称可以存在。
但是,如果有两次相同的指定,那么他们的成绩应该不同。
在上述情况中,指定24和25是重复的。
到目前为止我尝试了什么:
$counts = array_count_values($a1);
$filtered = array_filter($a1, function ($value) use ($counts) {
return $counts[$value] > 1;
});
$filtered
数组给出了重复索引号。
$filtered
(
[0] => 24
[1] => 25
[4] => 24
[5] => 25
)
我想检查A2数组中相同索引的值是否也是重复的。在这种情况下,指定24在相同指数的A2中具有相同的等级。
答案 0 :(得分:2)
检查A2数组中相同索引处的值是否重复 太
使用array_filter
,array_count_values
,array_intersect_key
,array_flip
和array_unique
函数的解决方案:
$a1 = [0 => 24, 1 => 25, 2 => 26, 3 => 27, 4 => 24, 5 => 25];
$a2 = [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 1, 5 => 10];
// getting all duplicate designation values from $a1 array
$counts = array_filter(array_count_values($a1), function($v){ return $v > 1; });
$dup_designations = [];
// iterating through all duplicate 'designation' items from $a1 array
foreach ($counts as $k => $v) {
// obtaining respective items from $a2 array by key intersection
// with current designation items sequence
$grades = array_intersect_key($a2, array_flip(array_keys($a1, $k)));
// check if found duplicates within $a2 array have the same value
if (count(array_unique($grades)) != count($grades)) {
$dup_designations[] = $k;
}
}
print_r($dup_designations);
输出:
Array
(
[0] => 24
)