dongzhijing8202 2018-01-20 11:57
浏览 73
已采纳

如何精确匹配php中的两个数组中的键和值[重复]

This question already has an answer here:

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

</div>
  • 写回答

1条回答 默认 最新

  • duanlu1908 2018-01-20 12:04
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?