You state that you want recursive differences of keys that works bidirectionally, so given your input:
$a = array('a' => 'a', 'b' => 'b', 'c' => array('d' => 'd', 'e' => 'e'));
$b = array('a' => 'a', 'b' => 'b', 'c' => array('t' => 'd', 'e' => 'e'));
You should get the output:
$output = array('c'=>array('d'=>'d','t'=>'d'));
//or
$output = array('t'=>'d','d'=>'d');
The below method will return the first version of the output. But, you said in your question, that it should just ouput t, which wouldn't make sense because then it could not possibly work bidirectionally (since the key d also does not match).
/** return an array that contains keys that do not match between $array and $compare, checking recursively.
* It's bi-directional so it doesn't matter which param is first
*
* @param $array an array to compare
* @param $compare another array
*
* @return an array that contains keys that do not match between $array and $compare
*/
function keyMatch($array,$compare){
$output = array();
foreach ($array as $key=>$value){
if (!array_key_exists($key,$compare)){
//keys don't match, so add to output array
$output[$key] = $value;
} else if (is_array($value)||is_array($compare[$key])){
//there is a sub array to search, and the keys match in the parent array
$match = keyMatch($value,$compare[$key]);
if (count($match)>0){
//if $match is empty, then there wasn't actually a match to add to $output
$output[$key] = $match;
}
}
}
//Literally just renaiming $array to $compare and $compare to $array
// Why? because I copy-pasted the first foreach loop
$compareCopy = $compare;
$compare = $array;
$array = $compareCopy;
foreach ($array as $key=>$value){
if (!array_key_exists($key,$compare)){
$output[$key] = $value;
} else if (is_array($value)||is_array($compare[$key])){
$match = keyMatch($value,$compare[$key]);
if (count($match)>0){
$output[$key] = $match;
}
}
}
return $output;
}
$a = array('a' => 'a', 'b' => 'b', 'c' => array('d' => 'd', 'e' => 'e'));
$b = array('a' => 'a', 'b' => 'b', 'c' => array('t' => 'd', 'e' => 'e'));
print_r(keyMatch($a,$b));
Oh, and here's an example of it working on your small, example input.
Array1: [1,2,3,2], Array 2: [3,2,3,1,3], current output: xy, expected output: zsomething like this.