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

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题