比较两个阵列,其中值是不相同的顺序 [英] compare two arrays where values are not in same order

查看:135
本文介绍了比较两个阵列,其中值是不相同的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有做这样的事情<一的快速方法href=\"http://stackoverflow.com/questions/15709494/compare-two-arrays-with-the-same-value-but-with-a-different-order\">Compare两个数组具有相同的价值,但在PHP中不同的订单?

我有阵列具有潜在相同的数据,但在不同的顺序,我只需要看看他们是否一致。

OK,原来我回来​​的对象,而不是一个数组,我猜...

 对象(学说\\ ORM \\ PersistentCollection)#560(9)等。

嗯......或许会最简单的方法来遍历集合的内容,以创建自己的数组,然后比较喜欢你的所有建议?

只是增加code为我的最终解决方案。

  //查找出来,如果​​容器接收mediasync
        $ toSync =阵列();
        的foreach($ C-GT&; getVideosToSync()作为$ V){
            $ toSync [] = $ V-&GT;的getId();
        }        $ INSYNC =阵列();
        的foreach($ C-GT&; getVideosInSync()作为$ V){
            $ INSYNC [] = $ V-&GT;的getId();
        }        $ noDiff =和array_diff($ toSync,$ INSYNC);
        $ sameLength =计数($ toSync)===计数($ INSYNC);        如果(空($ noDiff)及&放大器; $ sameLength){
           $ containerHelper [$ C-&GT;的getId()] ['同步'] = FALSE;
        }
        其他{
            $ containerHelper [$ C-&GT;的getId()] ['同步'] =真;
        }


解决方案

只需使用的 排序() &安培;然后用差异与它们 和array_diff()

 #设置测试数据。
$数组1 =数组(1,2,3,4,9,10,11);
$数组2 =阵列(3,4,2,6,7);#复制数组到新阵列进行排序/测试。
$ array1_sort = $数组1;
$ array2_sort = $数组2;#排序的数组。
排序($ array1_sort);
排序($ array2_sort);#DIFF排序的数组。
$ =和array_diff和array_diff($ array1_sort,$ array2_sort);#检查阵列是相同的长度或没有。
$ length_diff =(计数($数组1) - 数($数组2));
$ DIFFERENT_LENGTH =($ length_diff!= 0)?真假;#检查数组是不同的。
$ ARE_THEY_DIFFERENT =($和array_diff→1)?真假;如果($ DIFFERENT_LENGTH){
  回声'的长度不同:。 $ length_diff;
}
其他{
  回声他们具有相同的长度。';
}
呼应'&LT; BR /&GT;';如果($ ARE_THEY_DIFFERENT){
  呼应他们是不同的:。爆(,,$和array_diff);
}
其他{
  呼应他们没有什么两样。;
}
呼应'&LT; BR /&GT;';

Is there a fast way of doing something like this Compare two arrays with the same value but with a different order in 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;    
        }

解决方案

Just get them in a uniform order using sort() & then diff with them with array_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 />';

这篇关于比较两个阵列,其中值是不相同的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆