dsgdg46465 2016-07-28 06:53
浏览 438
已采纳

如何检查两个数组是否包含相同的值?

I have two arrays and I have to compare them against each other:

$a = array('BRANCH','ADDRESS','MOBILE','NAME');
$b = array('BRANCH','ADDRESS','MOBILE','NAME');

$a == $b → true

But when the values are in a different order, this comparison doesn't work:

$a = array('BRANCH','ADDRESS','MOBILE','NAME');
$b = array('NAME','BRANCH','MOBILE','ADDRESS');

$a == $b → false

How do i compare these two arrays to see if both contain the same values, regardless of their order?

  • 写回答

2条回答 默认 最新

  • dpoxk64080 2016-07-28 07:06
    关注

    I would do array_diff() which check difference between two arrays.

    $areEqual = array_diff($a, $b) === array_diff($b, $a);
    

    or

    $areEqual = !(array_diff($a, $b) || array_diff($b, $a));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题
  • ¥15 微信开发者工具vant组件
  • ¥15 iPhone iCloud云盘资料删除,MAC丢失50多G桌面资料
  • ¥20 希望各位学长可以给我答疑
  • ¥20 Linux实现: 客户端(cli01)通过TCP(或UDP)连接到聊天服务器(serv)进行聊天。
  • ¥15 (标签-matlab|关键词-m3)