Check if two arrays have the same values

后端 未结 9 1396
既然无缘
既然无缘 2020-12-15 02:45
[2,5,3]    

[5,2,3]

They are equal because they have the same values, but not in the same order. Can I find out that without using a foreach loop

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 03:29

    $array1 = array(2,5,3);
    $array2 = array(5,2,3);
    $result = array_diff($array1, $array2);
    if(empty($result))
    {
       echo "Both arrays are equal.";
    }
    else
    {
       echo "Both arrays are different.";
    }
    

提交回复
热议问题