comparing arrays in php, without caring for the order

前端 未结 5 765
借酒劲吻你
借酒劲吻你 2020-12-10 03:42

I have two arrays, $a and $b here, and need to check if they contain exactly the same elements (independently of the order). I am thinking of using

if (sizeo         


        
5条回答
  •  醉酒成梦
    2020-12-10 04:23

    Just for your amusement I'll add an example that demonstrates that your conditions is not correct:

    
    

    Test it.

    I would suggest using a different model. Maybe adding the elements as keys of the array, but this is possible only if they are integers or strings.

    $arr['itemA'] = true;
    $arr['itemB'] = true;
    

    This will enforce uniqueness. With this model you can use your condition on array_keys($arr).

提交回复
热议问题