0

For example, I want a function that returns me true for the following two inputs:

array('4','5','2') 
array('4','5','2')
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
surendra
  • 13
  • 3

3 Answers3

1

You have a couple of options depending on what you want:

just use a straight if

 if($array === $array2)

or you can use array_diff which will give you an output array of any differences.

 $diff = array_diff($array, $array2)
DevDonkey
  • 4,835
  • 2
  • 27
  • 41
0

Yes, You can check that both arrays are the same or not using the array_diff function.

$a1=array("4","5","2");
$a2=array("4","5","2");

$result=array_diff($a1,$a2);
print_r($result);

If Both arrays are not the same than it will return difference else return a blank array.

TrickStar
  • 229
  • 4
  • 19
0

If the 2 arrays are "identical", meaning that types are the same, than you can use the triple = comparison. If types are not identical, you could use the double = comparison.

$array1 = array('4', '5', '2');
$array2 = array('4', '5', '2');

var_dump($array1 == $array2); // true;
var_dump($array1 === $array2); // true;

VS

$array1 = array('4', '5', '2');
$array2 = array(4, 5, 2);

var_dump($array1 == $array2); // true;
var_dump($array1 === $array2); // false;
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55