php怎么比较两个数组是否相等

WBOY
Release: 2016-06-13 10:30:36
Original
1142 people have browsed it

php如何比较两个数组是否相等
有两个数组
$a = array('aaa','bbb','ddd','aaa');

$b = array('aaa','ddd','aaa','bbb');

如何比较这两个数组是否相等,前提是不能使用sort()之类的内置函数进行排序。

如果一定要使用内置函数,哪一个排序函数速度最快?



有兴趣的可以看一下下面这个问题:就是在一个数字数组中,找出最大的连续集合
例如:[1,2,5,6,7,8]最大的连续集合是[5,6,7,8]而不是1,2
[69,36,23,65,66,67,45]最大的连续集合是[65,66,67]


------解决方案--------------------
$a = array('aaa','bbb','ddd','aaa');
$b = array('aaa','ddd','aaa','bbb');

$c = array_diff($a, $b);

print_r($c);//输出array()

-----------------------------------------
$a = array('aaa1','bbb','ddd','aaa');
$b = array('aaa','ddd','aaa','bbb');

$c = array_diff($a, $b);

print_r($c);//输出array('aaa1')
------解决方案--------------------

PHP code
<?php $a=array(1,2,5,6,7,8);$b=array(69,36,23,65,66,67,45);$c=array(3,4,7,8,10,13,14);function newarray($arr){$temp1=array();$temp2=array();$temp3=array(); for($i=1;$i<count($arr);$i++){     if($arr[$i] == $arr[$i-1] + 1) {        $temp1[$i-1] = $arr[$i-1];        $temp1[$i] = $arr[$i];    }else {        $temp2=$temp1;        $temp1=array();    }    $temp3= (count($temp2) > count($temp1)) ? $temp2 : $temp1 ; }; return $temp3;}var_dump(newarray($a));echo "<br>";var_dump(newarray($b));echo "<br>";var_dump(newarray($c));//不能正确返回$c,因为$c可能有3,4/7,8/13,14三种?><div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template