在多维数组中查找重复值

时间:2021-06-07 12:58:45

I have an array that looks similar to this:

我有一个类似的数组:

Array
(
    [0] => Array
        (
            [0] => Model2345
            [1] => John Doe
            [2] => SN1234
        )

    [1] => Array
        (
            [0] => Model2345
            [1] => John Doe
            [2] => SN3456
        )

    [2] => Array
        (
            [0] => Model1234
            [1] => Jane Doe
            [2] => SN3456
        )
)

I want to have a way to check for duplicate values for keys [1] (the John Doe/Jane Doe key) and [2] (the SNxxxx key) in php, but ignore duplicates for key [0]. How can this be accomplished?

我希望有一种方法来检查键的重复值[1](John Doe/Jane Doe key)和php中的[2](SNxxxx键),但是忽略密钥的重复值[0]。这是如何实现的呢?

3 个解决方案

#1


0  

You Can use array_unique($array);, But it is return unique element of array, In your case return one element only.

您可以使用array_unique($array);但是它返回唯一的数组元素,在您的情况中只返回一个元素。

#2


0  

Try this:

试试这个:

$array = your_array();

$current = current($array);
foreach($array as $key => $val){
 $duplicate[$key] = array_intersect($current, $val);
}

echo($duplicate);

#3


0  

This question has already been answered here. The following is the code from the accepted answer of that question.

这个问题已经在这里得到了解答。下面是该问题的公认答案的代码。

It utilizes the array_intersect() function.

它使用array_intersect()函数。

<?php
$array = array(array("test data","testing data"), array("new data","test data"), array("another data", "test data", "unique data"));
$result = array();

$first = $array[0];
for($i=1; $i<count($array); $i++)
{
    $result = array_intersect ($first, $array[$i]);
    $first = $result;
}
print_r($result);
?>

OUTPUT:

输出:

Array ( [0] => test data )

数组([0]=>测试数据)

#1


0  

You Can use array_unique($array);, But it is return unique element of array, In your case return one element only.

您可以使用array_unique($array);但是它返回唯一的数组元素,在您的情况中只返回一个元素。

#2


0  

Try this:

试试这个:

$array = your_array();

$current = current($array);
foreach($array as $key => $val){
 $duplicate[$key] = array_intersect($current, $val);
}

echo($duplicate);

#3


0  

This question has already been answered here. The following is the code from the accepted answer of that question.

这个问题已经在这里得到了解答。下面是该问题的公认答案的代码。

It utilizes the array_intersect() function.

它使用array_intersect()函数。

<?php
$array = array(array("test data","testing data"), array("new data","test data"), array("another data", "test data", "unique data"));
$result = array();

$first = $array[0];
for($i=1; $i<count($array); $i++)
{
    $result = array_intersect ($first, $array[$i]);
    $first = $result;
}
print_r($result);
?>

OUTPUT:

输出:

Array ( [0] => test data )

数组([0]=>测试数据)