views:

54

answers:

6
$temp = array();
function show_keys($ar)
{
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            show_keys ($ar[$k]);
        }
    }

    return $temp;
}

I tried using that function but it still only returns the first key.

+1  A: 

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.

codaddict
No difference, seems like it was accepting the $temp in the first place.
gAMBOOKa
You're probably right, that was one step closer to the solution.
gAMBOOKa
+2  A: 

The main problem is that you are throwing away the results of the recursive show_keys() calls. You don't do anything with the return value.

Comments are inline.

function show_keys($ar)
{
    // Create new temp array inside function so each recursive call gets
    // a separate instance.
    $temp = array();

    foreach ($ar as $k => $v )
    {
        $temp[] = $k;

        // Use $v instead of $ar[$k].
        if (is_array($v))
        {
            // Combine results of recursive show_keys with $temp.
            $temp = array_merge($temp, show_keys($v));
        }
    }

    return $temp;
}
John Kugelman
+2  A: 

This might do the trick?

You'd have to either bring in $temp as a global, or pick up the returned value from each recursion. And we'd want to avoid global variables, so we merge the values from each recursion call with previous gathered values.

function show_keys($ar)
{
    $temp = array();
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            $temp = array_merge(show_keys ($ar[$k]), $temp);
        }
    }

    return $temp;
}
Alexander Sagen
Yes, it did.. thank you mister!
gAMBOOKa
A: 

Ofcause this function is infinite. But my task is to help you)

function show_keys($ar, $temp = array())
{    
    if (!empty($ar)) {
    foreach ($ar as $k => $v )
    {
        $temp[] = $k;
        if (is_array($ar[$k]))
        {
            $temp += show_keys($ar[$k], $temp);
        }
    }
    }

    return $temp;
}
Alexander.Plutov
+1  A: 

Using SPL, looping over the keys is quite easy (store them in another array if you wish):

<?php
$arr = array_fill(0,8,range(0,3));
var_dump($arr);
foreach( new RecursiveIteratorIterator(
    new RecursiveArrayIterator($arr),
    RecursiveIteratorIterator::SELF_FIRST)
  as $key => $value){
        var_dump($key);
}
?>
Wrikken
A: 

I see a lot of overly complicated solutions here....

function array_keys_r($array) {
  $keys = array_keys($array);

  foreach ($array as $i)
    if (is_array($i))
      $keys = array_merge($keys, array_keys_r($i));

  return $keys;
}
meagar