问题
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