Compare to values of two arrays in PHP

前端 未结 6 1376
南方客
南方客 2021-01-20 14:56

Hi i want to compare all the values of 2 arrays and end up with a true or false . I am using the code below and would of thought that the result would be false . but that is

6条回答
  •  再見小時候
    2021-01-20 15:00

    array_diff gives which elements are in $before but not $after. Since both arrays consist of '0' and '1', it returns an empty array.

    What you're looking for is array_diff_assoc, which looks at keys and values together.

    Do note, the output you get will not be Array( [0] => 0 ), but rather Array( [0] => 1 ), as it gives the elements from the first array that doesn't exist in the other one.

    If you wish the other output, you'll need to do array_diff_assoc($after, $before).

提交回复
热议问题