PHP: Built-in function to check whether two Array values are equal ( Ignoring the Order)

后端 未结 7 894
刺人心
刺人心 2020-12-30 01:34

Is there a built-in function for PHP for me to check whether two arrays contain the same values ( order not important?).

For example, I want a function that returns

7条回答
  •  旧巷少年郎
    2020-12-30 02:02

    You can use array_diff.

    $a = array('4','5','2');
    $b = array('2','4','5');
    
    if(count(array_diff($a, $b)) == 0) {
      // arrays contain the same elements
    } else {
      // arrays contain different elements
    }
    

    However, a problem with this approach is that arrays can contain duplicate elements, and still match.

提交回复
热议问题