比较两个数组,其中值的顺序不相同

时间:2022-06-10 12:15:26

Is there a fast way of doing something like this Compare two arrays with the same value but with a different order in PHP?

是否有一种快速的方法来做这样的事情,比较具有相同值但在PHP中顺序不同的两个数组?

I have arrays with potentially same data but in different order and I just need to see whether they are identical.

我有可能具有相同数据但顺序不同的数组,我只需要看看它们是否相同。

OK, turns out I get back an object and not an array, I guess...

我得到的是一个对象而不是数组。

object(Doctrine\ORM\PersistentCollection)#560 (9) etc.

hmm... Would the easiest way perhaps to iterate over the contents of the collection in order to create my own array and then compare like you all suggested?

嗯…最简单的方法可能是遍历集合的内容以创建我自己的数组,然后像大家建议的那样进行比较吗?

Just adding code for my final solution

只是为我的最终解决方案添加代码

        //Find out if container receives mediasync
        $toSync = array();
        foreach($c->getVideosToSync() as $v) {
            $toSync[] = $v->getId();
        }

        $inSync = array();
        foreach($c->getVideosInSync() as $v) {
            $inSync[] = $v->getId();
        }

        $noDiff = array_diff($toSync, $inSync);
        $sameLength = count($toSync) === count($inSync);

        if( empty($noDiff) && $sameLength ) {
           $containerHelper[$c->getId()]['syncing'] = false;
        }
        else {
            $containerHelper[$c->getId()]['syncing'] = true;    
        }

2 个解决方案

#1


-2  

Just get them in a uniform order using sort() & then diff with them with array_diff().

只需使用sort()以统一的顺序获取它们,然后使用array_diff()对它们进行diff。

# Set the test data.
$array1 = array(1,2,3,4,9,10,11);
$array2 = array(3,4,2,6,7);

# Copy the arrays into new arrays for sorting/testing.
$array1_sort = $array1;
$array2_sort = $array2;

# Sort the arrays.
sort($array1_sort);
sort($array2_sort);

# Diff the sorted arrays.
$array_diff = array_diff($array1_sort, $array2_sort);

# Check if the arrays are the same length or not.
$length_diff = (count($array1) - count($array2));
$DIFFERENT_LENGTH = ($length_diff != 0) ? true : false;

# Check if the arrays are different.
$ARE_THEY_DIFFERENT = ($array_diff > 1) ? true : false;

if ($DIFFERENT_LENGTH) {
  echo 'The are different in length: ' . $length_diff;
}
else {
  echo 'They have the same length.';
}
echo '<br />';

if ($ARE_THEY_DIFFERENT) {
  echo 'They are different: ' . implode(', ', $array_diff);
}
else {
  echo 'They are not different.';
}
echo '<br />';

#2


0  

I solved it the following way:

我用下面的方法解决了这个问题:

<?php

$arrayOld=array(
    '1'=>'32',
    '2'=>'34',
    '3'=>'36',
    '4'=>'38',
    '5'=>'40',
    '6'=>'42',
    '7'=>'44',
);

$arrayNew=array(
    '2'=>'32',
    '1'=>'34',
    '3'=>'36',
    '4'=>'38',
    '5'=>'46',
    '6'=>'42',
    '7'=>'44',
);

/**
 * Here we check if there is any difference in keys or values in two arrays
 * array_intersect_assoc - returns values that are same in both arrays checking values as well as keys
 * array_diff returns the difference between the arrayNew values and those same values in both arrays, returned by array_intersect_assoc
 */
$result = array_diff($arrayNew,array_intersect_assoc($arrayOld, $arrayNew));
print_r($result);


//result is:
Array ( 
    [2] => 32, 
    [1] => 34,
    [5] => 46,
)

/** We can see, that the indexes are different for values 32 and 34 * And the value for index 5 has also changed from 40 to 46 */

/**我们可以看到,32和34 *的索引不同,而索引5的索引值也从40变成了46 */

#1


-2  

Just get them in a uniform order using sort() & then diff with them with array_diff().

只需使用sort()以统一的顺序获取它们,然后使用array_diff()对它们进行diff。

# Set the test data.
$array1 = array(1,2,3,4,9,10,11);
$array2 = array(3,4,2,6,7);

# Copy the arrays into new arrays for sorting/testing.
$array1_sort = $array1;
$array2_sort = $array2;

# Sort the arrays.
sort($array1_sort);
sort($array2_sort);

# Diff the sorted arrays.
$array_diff = array_diff($array1_sort, $array2_sort);

# Check if the arrays are the same length or not.
$length_diff = (count($array1) - count($array2));
$DIFFERENT_LENGTH = ($length_diff != 0) ? true : false;

# Check if the arrays are different.
$ARE_THEY_DIFFERENT = ($array_diff > 1) ? true : false;

if ($DIFFERENT_LENGTH) {
  echo 'The are different in length: ' . $length_diff;
}
else {
  echo 'They have the same length.';
}
echo '<br />';

if ($ARE_THEY_DIFFERENT) {
  echo 'They are different: ' . implode(', ', $array_diff);
}
else {
  echo 'They are not different.';
}
echo '<br />';

#2


0  

I solved it the following way:

我用下面的方法解决了这个问题:

<?php

$arrayOld=array(
    '1'=>'32',
    '2'=>'34',
    '3'=>'36',
    '4'=>'38',
    '5'=>'40',
    '6'=>'42',
    '7'=>'44',
);

$arrayNew=array(
    '2'=>'32',
    '1'=>'34',
    '3'=>'36',
    '4'=>'38',
    '5'=>'46',
    '6'=>'42',
    '7'=>'44',
);

/**
 * Here we check if there is any difference in keys or values in two arrays
 * array_intersect_assoc - returns values that are same in both arrays checking values as well as keys
 * array_diff returns the difference between the arrayNew values and those same values in both arrays, returned by array_intersect_assoc
 */
$result = array_diff($arrayNew,array_intersect_assoc($arrayOld, $arrayNew));
print_r($result);


//result is:
Array ( 
    [2] => 32, 
    [1] => 34,
    [5] => 46,
)

/** We can see, that the indexes are different for values 32 and 34 * And the value for index 5 has also changed from 40 to 46 */

/**我们可以看到,32和34 *的索引不同,而索引5的索引值也从40变成了46 */