How to check if two indexed arrays have same values even if the order is not same in PHP?

后端 未结 2 1777
名媛妹妹
名媛妹妹 2021-01-22 12:22

I want to compare two indexed arrays as such that values will be same for two arrays but order may differ, for example i tried doing this but it simply doesn\'t work.

Ex

2条回答
  •  礼貌的吻别
    2021-01-22 13:13

    $a = array(1,2,3,4,5);
    $b = array(1,3,2,5,3,4);
    
    
    if(count($a) == count($b) && count(array_diff($a, $b)) == 0){
        echo "A";
    }
    

    Need to do a length check or the two arrays above would come out the same.

    edit: better solution.

提交回复
热议问题