Archive for the ‘comparison’ tag

Array comparison in PHP

without comments

While working on a recent project, I had to do some array comparison. I did array comparison by doing the following:

$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.

//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.";
}

Written by admin

July 9th, 2009 at 10:27 am

Posted in Development

Tagged with ,