且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP获取所有重复值的键

更新时间:2023-02-23 13:34:53

获取所有值的所有键很容易.

Getting all the keys for all the values is easy.

foreach ($array as $key => $value) {
    $result[$value][] = $key;
}

但是,这不仅会得到重复的值.有不同的方法可以做到这一点.一种方法是过滤结果,使其仅显示具有多个键的值.

But that doesn't get only the duplicate values. There are different ways to do that. One way is by filtering the result to only show values with more than one key.

$result = array_filter($result, function($item) {
    return count($item) > 1;
});