PHPUnit: assert two arrays are equal, but order of elements not important
You can use the assertEqualsCanonicalizing
or assertEqualsIgnoringCase
methods to compare two arrays and ignore the order of the elements.
$expected = [3, 2, 1];
$actual = [1, 2, 3];
$this->assertEqualsCanonicalizing($expected, $actual);
Watch a video course
Learn object oriented PHP
Alternatively, you can sort both arrays before comparing them using assertEquals
method
$expected = [3, 2, 1];
sort($expected);
$actual = [1, 2, 3];
sort($actual);
$this->assertEquals($expected, $actual);