While working on a recent project, I had to do some array comparison. I did array comparison by doing the following:
1 2 |
$firstArr = array(2,4,8); $secondArr = array(2,8,4); |
According to my requirements the above two arrays are same but if we compare it in PHP, it would not think so due to the presence of value on different indexes. To solve that, I first sorted and then compared these arrays, which gave me the required result.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//sort the arrays using php's sort function sort($firstArr); sort($secondArr); //compare the arrays if ( $firstArr === $secondArr) { echo "Yes. They are equal"; } else { echo "No. They are not."; } |