I have an array like the following:
Array ( [1g27] => Array ( [42] => AAAAA [52] => BBBBB [4] => 2g4 ) [2g4] => Array ( [9] => CCCCC [14] => 3g14 [15] => 3g15 [20] => DDDDD [34] => EEEEE ) [3g14] => Array ( [49] => FFFFF ) [3g15] => Array ( [50] => GGGGG ) )
I would like it to become an array like the following:
Array ( [1g27] => Array ( [42] => AAAAA [52] => BBBBB [2g4] => Array ( [9] => CCCCC [3g14] => Array ( [49] => FFFFF ) [3g15] => Array ( [50] => GGGGG ) [20] => DDDDD [34] => EEEEE ) )
Notice that the values of the original array are also keys found in the original array. When there is a match between a value and an array, I want the array to replace the value (and rename that value's key).
I have tried foreach and array_walk_recursive, but I can't figure it out. (See following attempt which only goes 1 level deep.)
function inflate($flatree, $array) { global $inflatedtree; foreach ($array as $arraykey => $arrayvalue) { $inflatedtree[$arraykey] = $arrayvalue; if (array_key_exists($arrayvalue, $flatree)) { $inflatedtree[$arrayvalue] = $flatree[$arrayvalue]; inflate($flatree, $inflatedtree[$arrayvalue]); } } } inflate($flatree, $flatree['1g27']);
using this function, however, gives me this:
Array ( [42] => AAAAA [52] => BBBBB [2g4] => Array ( [9] => CCCCC [14] => 3g14 [15] => 3g15 [20] => DDDDD [34] => EEEEE ) [3g14] => Array ( [49] => FFFFF ) [3g15] => Array ( [50] => GGGGG ) )