Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array comparison

Tags:

arrays

php

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.

like image 564
Semas Avatar asked Aug 22 '10 10:08

Semas


1 Answers

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.

like image 109
soulmerge Avatar answered Sep 19 '22 04:09

soulmerge