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 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题