PHP -如何比较两个数组并删除重复的值

时间:2022-06-09 08:39:18

So this is whats bothering me.

这就是困扰我的问题。

I have two arrays:

我有两个数组:

$array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
$array2 = array('value1'   ,'demo' ,'value2'   ,'some' ,'value3');

Now I want to compare these two array's, and remove all duplicate values.
At the end I want this two array-s but without 'demo' and 'some' values in them.
I want to remove all values from array-s that have the same index key and value.
Array's will always have same number of values and indexes, I only want to compare them and remove entries that have the same index key and value, from both of them.

现在我要比较这两个数组,并删除所有重复的值。最后,我想要这两个数组,但没有“demo”和“some”值。我想从具有相同索引键和值的array-s中删除所有值。数组总是有相同数量的值和索引,我只想对它们进行比较,并从它们中删除具有相同索引键和值的条目。

I'm doing something like this now:

我现在正在做这样的事情:

$clean1 = array();
$clean2 = array();    

foreach($array1 as $key => $value)
{
    if($value !== $array2[$key])
    {
        $clean1[$key] = $value;
        $clean2[$key] = $array2[$key];
    }
}

var_export($clean1);
echo "<br />";
var_export($clean2);

And this works! But im wondering is there any other way of doing this? Maybe without using foreach loop? Is there more elegant way of doing this?

这工作!但是我想知道还有其他的方法吗?也许不用foreach循环?有更优雅的方法吗?

7 个解决方案

#1


22  

array_unique( array_merge($arr_1, $arr_2) );

or you can do:

或者你可以做的:

$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);

i guess...

我猜……

#2


4  

You can use the function array_diff in PHP that will return and array containing the keys that are the same between the two arrays.

您可以在PHP中使用array_diff函数,它将返回包含两个数组之间相同的键的数组。

$clean1 = array_diff($array1, $array2);

http://php.net/manual/en/function.array-diff.php

http://php.net/manual/en/function.array-diff.php

#3


2  

$clean1 = array_diff($array1, $array2); 
$clean2 = array_diff($array2, $array1); 
$final_output = array_merge($clean1, $clean2);

#4


0  

Since $array1 and $array2 are always the same length, you could do something like this:

因为$array1和$array2的长度是一样的,你可以这样做:

<?php

    $array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
    $array2 = array('value1'   ,'demo' ,'value2'   ,'some' ,'value3');

    $map = array_combine($array1,$array2);
    $map = array_filter($map ,function ($item) use (&$map) {
        $keep_data = ($item != key($map));
        next($map);
        return $keep_data;
    });

    $clean1 = array_keys($map);
    $clean2 = array_values($map);

    var_export($clean1);
    echo "<br />";
    var_export($clean2);

?>

Is it better? You decide

这是更好的吗?你决定

#5


0  

what about?

关于什么?

$res = array_diff($array1, $array_intersect($array1, $array2));
$reindexed = array_combine(array_keys($res), array_values($res));

or simply way if keys are not needed

或者简单地说,如果不需要键的话。

array_values(array_diff($array1, $array_intersect($array1, $array2)));

#6


0  

Your solution is definitely the most "elegant" (meaning the easiest to read, and least amount of code), but here is another solution that uses array_diff_ukey(). It preserves the keys, and puts them in incremental order, as you requested.

您的解决方案肯定是最“优雅”的(意味着最容易阅读,代码量最少),但是这里有另一个使用array_diff_ukey()的解决方案。它保存密钥,并按照您的要求将它们按增量顺序排列。

$array1 = ['[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]'];
$array2 = ['value1'   ,'demo' ,'value2'   ,'some' ,'value3'];

$clean1 = $array1;

$clean2 = array_diff_ukey(
    $array2,
    $array1,
    // Need to have "&" so the values get set.
    function($a, $b) use (&$clean1, $array1, $array2) {

        // Use isset() just in case keys are not identical
        // or arrays are not the same length.
        if (isset($array2[$b]) && $array2[$b] === $array1[$b]) {
            unset($clean1[$b]);
        }

        return strcmp($array2[$a], $array1[$b]);

});

print_r($clean1);
print_r($clean2);

Will return this:

将返回:

Array
(
    [0] => [param1]
    [2] => [param2]
    [4] => [param3]
)
Array
(
    [0] => value1
    [2] => value2
    [4] => value3
)

Working example here.

工作的例子。

#7


0  

For example i have two array like below:

例如,我有两个数组,如下所示:

$array1=Array([0] => parking.jpeg);
$array2=Array ([0] => parking.jpeg [1] => logo.jpeg);

first compare from array1 to array2 like below, which will return null

首先将array1与array2进行比较,如下所示,它将返回null

$clean1 = array_diff($array1, $array2);

second compare from array2 to array1 which will return logo.jpeg

第二个比较是array2和array1,后者返回logo.jpeg

$clean2 = array_diff($array2, $array1);

now you can merge both differences in one array like below:

现在你可以合并两个不同的数组如下:

$final_output = array_merge($clean1, $clean2);

Output:Array ( [0] => logo.jpeg )

输出:数组([0]=> logo.jpeg)

#1


22  

array_unique( array_merge($arr_1, $arr_2) );

or you can do:

或者你可以做的:

$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);

i guess...

我猜……

#2


4  

You can use the function array_diff in PHP that will return and array containing the keys that are the same between the two arrays.

您可以在PHP中使用array_diff函数,它将返回包含两个数组之间相同的键的数组。

$clean1 = array_diff($array1, $array2);

http://php.net/manual/en/function.array-diff.php

http://php.net/manual/en/function.array-diff.php

#3


2  

$clean1 = array_diff($array1, $array2); 
$clean2 = array_diff($array2, $array1); 
$final_output = array_merge($clean1, $clean2);

#4


0  

Since $array1 and $array2 are always the same length, you could do something like this:

因为$array1和$array2的长度是一样的,你可以这样做:

<?php

    $array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
    $array2 = array('value1'   ,'demo' ,'value2'   ,'some' ,'value3');

    $map = array_combine($array1,$array2);
    $map = array_filter($map ,function ($item) use (&$map) {
        $keep_data = ($item != key($map));
        next($map);
        return $keep_data;
    });

    $clean1 = array_keys($map);
    $clean2 = array_values($map);

    var_export($clean1);
    echo "<br />";
    var_export($clean2);

?>

Is it better? You decide

这是更好的吗?你决定

#5


0  

what about?

关于什么?

$res = array_diff($array1, $array_intersect($array1, $array2));
$reindexed = array_combine(array_keys($res), array_values($res));

or simply way if keys are not needed

或者简单地说,如果不需要键的话。

array_values(array_diff($array1, $array_intersect($array1, $array2)));

#6


0  

Your solution is definitely the most "elegant" (meaning the easiest to read, and least amount of code), but here is another solution that uses array_diff_ukey(). It preserves the keys, and puts them in incremental order, as you requested.

您的解决方案肯定是最“优雅”的(意味着最容易阅读,代码量最少),但是这里有另一个使用array_diff_ukey()的解决方案。它保存密钥,并按照您的要求将它们按增量顺序排列。

$array1 = ['[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]'];
$array2 = ['value1'   ,'demo' ,'value2'   ,'some' ,'value3'];

$clean1 = $array1;

$clean2 = array_diff_ukey(
    $array2,
    $array1,
    // Need to have "&" so the values get set.
    function($a, $b) use (&$clean1, $array1, $array2) {

        // Use isset() just in case keys are not identical
        // or arrays are not the same length.
        if (isset($array2[$b]) && $array2[$b] === $array1[$b]) {
            unset($clean1[$b]);
        }

        return strcmp($array2[$a], $array1[$b]);

});

print_r($clean1);
print_r($clean2);

Will return this:

将返回:

Array
(
    [0] => [param1]
    [2] => [param2]
    [4] => [param3]
)
Array
(
    [0] => value1
    [2] => value2
    [4] => value3
)

Working example here.

工作的例子。

#7


0  

For example i have two array like below:

例如,我有两个数组,如下所示:

$array1=Array([0] => parking.jpeg);
$array2=Array ([0] => parking.jpeg [1] => logo.jpeg);

first compare from array1 to array2 like below, which will return null

首先将array1与array2进行比较,如下所示,它将返回null

$clean1 = array_diff($array1, $array2);

second compare from array2 to array1 which will return logo.jpeg

第二个比较是array2和array1,后者返回logo.jpeg

$clean2 = array_diff($array2, $array1);

now you can merge both differences in one array like below:

现在你可以合并两个不同的数组如下:

$final_output = array_merge($clean1, $clean2);

Output:Array ( [0] => logo.jpeg )

输出:数组([0]=> logo.jpeg)