dsgdg46465
dsgdg46465
2016-07-28 06:53

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

已采纳

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 dpoxk64080 5年前

    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));
    
    点赞 评论 复制链接分享
  • duan0504 duan0504 5年前
    sort($a);
    sort($b);
    if ($a==$b) {
    echo "arraysAreEqual";
    }
    
    点赞 评论 复制链接分享