0

I have two arrays i want to match exact both keys and values of arrays one of array be three dimensional or more Example

$arr1 = ['status'=>true,'message'=>'data saved']; 
$arr2 = ['status'=>true,'message'=>'data saved'];

in this scenario array return 1 but they are not equal

$arr1 = array("messagess"=>"data added","status" => true);
$arr2 = array("status" => true,'message'=>'data has been added'); 
echo count(array_intersect_assoc($arr1,$arr2));

Expected should be true if both exact match otherwise false. I have tried array_intersect() and other methods but failed.

Please Guide!

Thank in Advance

Raheel Aslam
  • 444
  • 1
  • 9
  • 28

1 Answers1

1

You could use array_intersect_assoc() and count the resulting number

  echo count(array_intersect_assoc($arr1,$arr2));

http://php.net/manual/en/function.array-intersect-assoc.php

If the number in count is the same of the number of index keys you want check the the two array are the same otherwise you get the number of key values that match

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • but count of this $arr1 = array("messagess"=>"data added","status" => true); $arr2 = array("status" => true,'message'=>'data has been added'); return 1 should return 0 – Raheel Aslam Jan 20 '18 at 13:02
  • 1 is correct .. in your sample you are looking for 2 .. so one key value match not 2..answer updated with a brief explanation .. for more look a t php manual in link – ScaisEdge Jan 20 '18 at 16:14