3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_diff_assoc_recursive( ...$arrays ) { $i = \count( $arrays ); while ( --$i ) { $p = $i - 1; if ( \is_array( $arrays[ $i ] ) && \is_array( $arrays[ $p ] ) ) { foreach ( $arrays[ $i ] as $key => &$value ) { if ( ! \array_key_exists( $key, $arrays[ $p ] ) ) { // If the value doesn't exist in previous array, pass it along. $arrays[ $p ][ $key ] = $value; continue; } if ( $value === $arrays[ $p ][ $key ] || ( \is_array( $value ) && ! array_diff_assoc_recursive( ...array_column( $arrays, $key ) ) ) ) { // If there's no diff with the previous array or no diff can be found recursively, remove it from all the next arrays. foreach ( range( $p, $i ) as $_i ) if ( \is_array( $arrays[ $_i ] ) ) unset( $arrays[ $_i ][ $key ] ); continue; } } } } return $arrays[0]; } $array1 = array("a" => "green", "b" => "brown", "c" => "bluea" ); $array2 = array("a" => "green", "b" => "brown", "c" => "blue" ); var_dump( array_diff_assoc( $array1, $array2 ), array_diff_assoc_recursive( $array1, $array2 ), array_diff_assoc( $array1 ), array_diff_assoc_recursive( $array1 ), ); $a = [ 'one' => [ 1 => 'test', 2 => [ 'one', 'five', 'two', [ 'diff' => 'this', 'three' => 'four' ], ], ], 'two' => [ 1 => 'test2', 'two' => [ 'one', 'two', 'three' => 'four', 5, ], ], 'three', ]; $b = [ 'one' => [ 1 => 'test', 2 => [ 'one', 'five', 'two', [ 'three' => 'four', 'diff' => 'this' ], // 'one', 'two', [ 'diff' => 'this', 'three' => 'four' ], 'five', ], ], 'two' => [ 'two' => [ 'one', 'two', 5 => 4, 'three' => 'four', // 'one', 'two', 'three' => 'four', 5, ], 1 => 'test2', ], 'three', 'four', ]; var_dump( array_diff_assoc_recursive( $a, $b ), );
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
array(1) { ["c"]=> string(5) "bluea" } array(1) { ["c"]=> string(5) "bluea" } array(3) { ["a"]=> string(5) "green" ["b"]=> string(5) "brown" ["c"]=> string(5) "bluea" } array(3) { ["a"]=> string(5) "green" ["b"]=> string(5) "brown" ["c"]=> string(5) "bluea" } array(2) { ["two"]=> array(2) { [1]=> string(5) "test2" ["two"]=> array(4) { [0]=> string(3) "one" [1]=> string(3) "two" ["three"]=> string(4) "four" [2]=> int(5) } } [1]=> string(4) "four" }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
28.79 ms | 408 KiB | 5 Q