开发者

PHP array comparison

开发者 https://www.devze.com 2023-01-12 09:01 出处:网络
How to compare 2 arrays with each other? For example i have array(\"a\", \"b\", \"c\") and array(\"a\", \"c\", \"b\") It would return true when they\'r开发者_开发百科e compared. But if one of the lett

How to compare 2 arrays with each other?

For example i have array("a", "b", "c") and array("a", "c", "b") It would return true when they'r开发者_开发百科e compared. But if one of the letters if not found in one of them it would return false. Order is not important.


You need to bring the content of both arrays into the same order prior to comparison:

sort($array1);
sort($array2);
// now you can compare as usual
if ($array1 == $array2) ...

Or use asort() if you want to maintain keys.


You can use:

if (empty(array_diff($array1, $array2)) {
    // do something
}
0

精彩评论

暂无评论...
验证码 换一张
取 消