如何检查与其他数组包含相同值的php数组
我有两个数组,分别是如何检查与其他数组包含相同值的php数组,php,arrays,Php,Arrays,我有两个数组,分别是$a和$b,其中数组的值相同,但元素的索引不同 $a Array ( [0] => there is bald spot on the inside or outside of tyre [1] => uneven tyre wear ) 但是当我使用$a==$b进行比较时,即使数组中的元素相同(只是元素的位置不同),它也会返回false 之前给出的解决方案是 $a = Array ( 0 => 'there is bald spot on t
$a
和$b
,其中数组的值相同,但元素的索引不同
$a
Array ( [0] => there is bald spot on the inside or outside of tyre [1] => uneven tyre wear )
但是当我使用$a==$b
进行比较时,即使数组中的元素相同(只是元素的位置不同),它也会返回false
之前给出的解决方案是
$a = Array (
0 => 'there is bald spot on the inside or outside of tyre',
1 => 'uneven tyre wear'
);
$b = Array (
0 => 'uneven tyre wear',
1 => 'there is bald spot on the inside or outside of tyre'
);
if(count(array_diff($a,$b)) == 0){
echo "both array are identical";
}
但是如果我从$a中删除一个元素
$a = Array (
0 => 'there is bald spot on the inside or outside of tyre'
//1 => 'uneven tyre wear'
);
如果使用上述解决方案,它仍然显示相同的结果。使用
输出:-
注意:-如果两个数组大小不同(一个有多个元素,另一个有少个元素),那么在使用array_diff()
检查此输出:-使用数组_diff()
使用数组_diff()
检查that@AnantSingh---AlivetoDie我刚刚发布到这个函数
$a = Array (
0 => 'there is bald spot on the inside or outside of tyre'
//1 => 'uneven tyre wear'
);
if(count(array_diff($a,$b)) == 0){
echo "both array are identical";
}
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2)
print_r($result);
?>