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

后端 未结 7 885
刺人心
刺人心 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:59

    If the arrays being compared consist of only strings and/or integers, array_count_values allows you to compare the arrays quickly (in O(n) time vs O(n log n) for sorting) by verifying that both arrays contain the same values and that each value occurs the same # of times in both arrays.

    if(array_count_values($a1) == array_count_values($a2)) {
        //arrays are equal
    }
    

提交回复
热议问题