php - 如何检查包含与其他数组相同值的php数组
问题描述
我有两个数组,它们是$a
和$b
,其中数组的值相同,但元素的索引不同。
$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 )
但是当我通过 using 进行比较时,即使数组中的元素相同$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'
);
如果使用上述解决方案,它仍然显示相同。
解决方案
if(count(array_diff($a,$b)) == 0){
echo "both array are identical";
}
注意:-如果两个数组大小不同(一个有更多元素,另一个有更少元素),那么您必须在使用时将更大的数组放在第一个位置array_diff()
检查此输出:- https://3v4l.org/9lNae
推荐阅读
- java - 如何将 BufferedReader 写入 slf4j 记录器
- c# - 传递强命名参数的最佳方法?
- java - Kotlin 属性未初始化,但为什么呢?
- javascript - 如何用变量定义范围?
- android - 我有这样的问题,没有看到库(import android.support.v7.widget.RecyclerView;)
- python - 如何在 Django 中将表单的初始值设置为 html 或模板语言?
- c++ - 在 Mac OS 上使用 C/C++ 获得鼠标跟踪速度
- sqlite - 为什么给出“参数计数不匹配”错误?
- java - 如何在架构级别监听 kafka 事件
- python - 我想为我的时间序列数据设置一个频率(我的数据以五分钟为单位)