PHP - 检查多维数组中的相同值 [英] PHP - Checking identical values within a multidimensional array

查看:75
本文介绍了PHP - 检查多维数组中的相同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组:

[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)是否与另一个数组相同.就像上面例子中的 input[0] 和 input[4] 一样.我如何在 php 中做到这一点?

I only want to check if the first 2 value (val1 and val2) in an array are identical to another array. Like input[0] and input[4] in the example above. How to I do this in php?

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

I don't want to remove the duplicated array, I just only to return the duplicated value for further use.

谢谢

推荐答案

你只需要存储这些键的数组:

You just need the array where you store those keys:

$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));

这篇关于PHP - 检查多维数组中的相同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆