我有这样的多维数组。
array:8 [
0 => array:8 [
"SERIAL" => "qwerty"
"BRANCH" => "TEST1"
]
1 => array:8 [
"SERIAL" => "qwer"
"BRANCH" => "TEST1"
]
2 => array:8 [ // RETURN THIS AS ERROR AS THE BRANCH "TEST1" has already "qwerty" serial
"SERIAL" => "qwerty"
"BRANCH" => "TEST2"
]
3 => array:8 [
"SERIAL" => "qwerty"
"BRANCH" => "TEST1"
]
]
我的目标是如果存在“ SERIAL”的重复值和不同的“ BRANCH”,则返回错误。
任何想法如何实现这一目标?
如果将序列和分支分组,则可以看到唯一计数是否大于一个。
这将输出所有重复项,而不仅仅是第一个。
foreach($arr as $sub){
$result[$sub['SERIAL']][] = $sub['BRANCH'];
}
foreach($result as $serial => $branches){
$unique = array_unique($branches);
if(count($unique) >1){
var_dump("ERROR: " . $serial, array_slice($unique,1));
}
}
输出:
string(13) "ERROR: qwerty"
array(2) {
[0]=>
string(5) "TEST2"
[1]=>
string(5) "TEST3" // I added that to test on multiple duplicates
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句