我的PHP应用程序中有一个如下所示的数组:
Array
(
[0] => Array
(
[name] => Name1
[language] => 1
)
[1] => Array
(
[name] => Name2
[language] => 1
)
)
如何检查值为1的“语言”是否尽可能有效地出现两次?
答案 0 :(得分:3)
$dupe = 0;
foreach($yourarray as $key => $val) {
if(array_key_exists($seen, $val['language'])) {
// a duplicate exists!
$dupe = 1;
// could do other stuff here too if you want,
// like if you want to know the $key with the dupe
// if all you care about is whether or not any dupes
// exist, you could use a "break;" here to early-exit
// for efficiency. To find all dupes, don't use break.
}
$seen[$val['language']] = 1;
}
// If $dupe still = 0 here, then no duplicates exist.
答案 1 :(得分:1)
尝试过PHP函数array_unique?
(阅读下面的评论/用户贡献说明,尤其是 regeda at inbox dot ru ,他为多维数组制作了一个递归函数)