<?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 ), );
You have javascript disabled. You will not be able to edit any code.