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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器