I'd like to check, whether an array has been changed compared to another array.
I'd like to get a boolean value for
- has changed (
true
) - has not changed (
false
)
Let's assume this is my original array:
$originalArray = [1,2,3,];
Now the boolean should be true
, if the $newArray
looks e.g. like:
Added item
$newArray = [1,2,3,4];
Removed item
$newArray = [1,2];
Different values
$newArray = [1,3,5];
My attempts
$originalArray = [1,2,3,];
$newArray = [1,2,3,4];
array_diff($newArray, $originalArray);
This works fine and the result is
[3 => 4,]
but for
<?php
$originalArray = [1,2,3,];
$newArray = [1,2,];
array_diff($newArray, $originalArray);
it does no longer work. I would have to switch $newArray
and $originalArray
in array_diff
.
I think doing something like
$result = count(array_diff($originalArray, $newArray)) === 0 && count(array_diff($newArray, $originalArray)) === 0;
is not the best try?