Your array $temp
is global. To make is accessible in the function you need :
global $temp;
at the start of the function.
Currently every invocation of the function is creating a new array with the name $temp
and when you finally return from the function to its caller, the $temp
you created in your first call is being returned, which has only the keys of your first level.
Note that using global variables is not good programming. You need to pass your array as argument to your recursive calls and modify the passed array by adding keys found in each iterations as Alexander and John have done.