Home » Php » PHP array comparison

PHP array comparison

Posted by: admin July 12, 2020 Leave a comment


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’re compared. But if one of the letters if not found in one of them it would return false. Order is not important.

How to&Answers:

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

// 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