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

后端 未结 7 888
刺人心
刺人心 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 01:55

    array_diff looks like an option:

    function array_equal($a1, $a2) {
      return !array_diff($a1, $a2) && !array_diff($a2, $a1);
    }
    

    or as an oneliner in your code:

    if(!array_diff($a1, $a2) && !array_diff($a2, $a1)) doSomething();
    

提交回复
热议问题