PHP - 在多维数组中检查相同的值

时间:2012-09-22 16:22:15

标签: php multidimensional-array

我有一个这样的数组:

[input] => Array (
    [0] => Array ( [val1] => 111 [val2] => 222 [val3] => 333 [day] => 444 )          
    [1] => Array ( [val1] => 111 [val2] => 221 [val3] => 333 [day] => 444 ) 
    [2] => Array ( [val1] => 111 [val2] => 223 [val3] => 333 [day] => 444 ) 
    [3] => Array ( [val1] => 111 [val2] => 224 [val3] => 333 [day] => 444 )
    [4] => Array ( [val1] => 111 [val2] => 222 [val3] => 333 [day] => 444 ) 
           ) 

我只想检查数组中的前2个值(val1和val2)是否与另一个数组相同。与上例中的输入[0]和输入[4]类似。我怎么在PHP中这样做?

我不想删除重复的数组,我只是为了返回重复的值以供进一步使用。

感谢

1 个答案:

答案 0 :(得分:0)

您只需要存储这些密钥的阵列:

$uniq = array();
foreach($input as $v) {
    $key = $v['val1'] . '-' . $v['val2'];
    if (!isset($uniq[$key]))
        $uniq[$key] = 0;
    else
        $uniq[$key]++;
}
print_r(array_filter($uniq));