[Solved] PHP: Show the difference in two arrays when elements out of order


You can use array_diff_assoc() to do this, it works out the difference between the two arrays, with a key check to verify that the keys are the same as well:

$a = ['id', 'name', 'age', 'gender'];
$b = ['id', 'age', 'name', 'gender'];

$expected = array_diff_assoc($a, $b);
$actual = array_diff_assoc($b, $a);

echo 'Expected = ', implode(', ', $expected), PHP_EOL;
echo 'Actual=", implode(", ', $actual), PHP_EOL;

Demo

1

solved PHP: Show the difference in two arrays when elements out of order