3v4l.org

run code in 300+ PHP versions simultaneously
<?php function consistsOfTheSameValues(array $A, array $B, $strict = false, $assumeEqual = false) { // check size of both arrays if (count($A) !== count($B)) { return false; } // optional pre-test that increases performance for most unequal arrays, but is worse for equal arrays if (! $assumeEqual) { foreach ($A as $a) { // check that expected value exists in the array if (! in_array($a, $B, $strict)) { return false; } } } foreach ($A as $a) { // check that expected value occurs the same amount of times in both arrays if (count(array_keys($A, $a, $strict)) !== count(array_keys($B, $a, $strict))) { return false; } } return true; } // A unit testing framework in a tweet. https://gist.github.com/mathiasverraes/9046427 function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);} // create two big arrays $big1 = []; for ($i = 0; $i < 999; $i++) { $big1[] = rand(); } $big2 = $big1; $big1[] = 0; $big2[] = 1; foreach ([false, true] as $ae) { foreach ([true, false] as $st) { echo '### ' . ($st ? 'strict (===)' : 'loose (==)' ) . ' - ' . ($ae ? 'skip' : 'use') . " in_array\n"; // positive tests it('consists of the same values', consistsOfTheSameValues([1], [1], $st, $ae) === true ); it('consists of the same values', consistsOfTheSameValues([1, 1], [1, 1], $st, $ae) === true ); it('consists of the same values', consistsOfTheSameValues(['1', 1], ['1', 1], $st, $ae) === true ); it('consists of the same values', consistsOfTheSameValues(['1', 1], [1, '1'], $st, $ae) === true ); it('consists of the same values', consistsOfTheSameValues([1, '1'], ['1', 1], $st, $ae) === true ); it('consists of the same values', consistsOfTheSameValues([1, '1'], [1, '1'], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['x' => 1], ['x' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['x' => 1], ['y' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['y' => 1], ['x' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['y' => 1, 'x' => 1], ['x' => 1, 'y' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['x' => 1, 'y' => 1], ['y' => 1, 'x' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['y' => 1, 'x' => 1], ['y' => 1, 'x' => 1], $st, $ae) === true ); it('consists of the same values with key', consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2], $st, $ae) === true ); it('consists of the same values for big1 and big1', consistsOfTheSameValues($big1, $big1, $st, $ae) === true ); it('consists of the same values for big2 and big2', consistsOfTheSameValues($big2, $big2, $st, $ae) === true ); // negative tests it('does not consist of the same values', consistsOfTheSameValues([1], [2], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([1], [1, 1], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([1, 1], [1], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([1], [1, 2], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([1], [2, 1], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([1, 2], [1], $st, $ae) === false ); it('does not consist of the same values', consistsOfTheSameValues([2, 1], [1], $st, $ae) === false ); it('does not consist of the same values with key', consistsOfTheSameValues(['x' => 1], ['x' => 2], $st, $ae) === false ); it('does not consist of the same values with key', consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 2], $st, $ae) === false ); it('does not consist of the same values with key', consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 2, 'y' => 1], $st, $ae) === false ); it('does not consist of the same values with key', consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1], $st, $ae) === false ); it('does not consist of the same values for big1 and big2', consistsOfTheSameValues($big1, $big2, $st, $ae) === false ); it('does not consist of the same values for big2 and big1', consistsOfTheSameValues($big2, $big1, $st, $ae) === false ); // test strictness $consists_of = ($st ? 'does not consist of the same strict values' : 'consists of the same loose values'); it($consists_of, consistsOfTheSameValues(['1'], [1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1], ['1'], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues(['1', 1], [1, 1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1, '1'], [1, 1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1, 1], ['1', 1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1, 1], [1, '1'], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues(['1', '1'], [1, 1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues(['1', '1'], ['1', 1], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues(['1', '1'], [1, '1'], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1, 1], ['1', '1'], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues(['1', 1], ['1', '1'], $st, $ae) !== $st ); it($consists_of, consistsOfTheSameValues([1, '1'], ['1', '1'], $st, $ae) !== $st ); } }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.25, 7.3.0 - 7.3.12, 7.4.0
### strict (===) - use in_array ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values for big1 and big1 ✔︎ It consists of the same values for big2 and big2 ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values for big1 and big2 ✔︎ It does not consist of the same values for big2 and big1 ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ### loose (==) - use in_array ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values for big1 and big1 ✔︎ It consists of the same values for big2 and big2 ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values for big1 and big2 ✔︎ It does not consist of the same values for big2 and big1 ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ### strict (===) - skip in_array ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values for big1 and big1 ✔︎ It consists of the same values for big2 and big2 ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values for big1 and big2 ✔︎ It does not consist of the same values for big2 and big1 ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ✔︎ It does not consist of the same strict values ### loose (==) - skip in_array ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values with key ✔︎ It consists of the same values for big1 and big1 ✔︎ It consists of the same values for big2 and big2 ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values with key ✔︎ It does not consist of the same values for big1 and big2 ✔︎ It does not consist of the same values for big2 and big1 ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values ✔︎ It consists of the same loose values

preferences:
76.58 ms | 430 KiB | 5 Q