PHP - Checking identical values within a multidimensional array

社会主义新天地 提交于 2019-12-25 00:16:36

问题


I have an array like this :

[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 ) 
           ) 

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.

thanks


回答1:


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


来源:https://stackoverflow.com/questions/12545555/php-checking-identical-values-within-a-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!