1

I'm trying to write a function that takes a multi-dimensional array as input and outputs a multi-line string of keys like the following

['key']['subkey']
['key']['another_subkey']
['key']['another_subkey']['subkey_under_subkey']
['key']['yet_another_subkey']
['another_key']['etc']

Here is my attempt. It has problems when you get to the second level.

function get_array_keys_as_string($array){
    $output = "";
    foreach($array as $k => $v){
        if(is_array($v)){
            $string = get_array_keys_as_string($v);
            $prepend = "['$k']";
            $string = $prepend.str_replace("\n","\n".$prepend, $string);
            $output .= $string;
        }
        else{
            $output .= "['$k']\n";
        }
    }
    return $output;
}

I know I need a recursive function, but so far my attempts have come up short.

3
  • Can you provide us with the desired output ? Commented Aug 22, 2014 at 19:49
  • The above is the desired output as a string. Commented Aug 22, 2014 at 19:50
  • I know it would output the values also, but if you just need to inspect an array to see the structure why not just print_r it? Commented Aug 22, 2014 at 20:00

3 Answers 3

1

To get the exact output you asked for use the following:

$arr = array(
    "key" => array(
        "subkey" => 1,
        "another_subkey" => array(
            "subkey_under_subkey" => 1
        ),
        "yet_another_subkey" => 1
    ),
    "another_key" => array(
        "etc" => 1
    )
);

function print_keys_recursive($array, $path = false) {
    foreach($array as $key=>$value) {       
        if(!is_array($value)) {
            echo $path."[".$key."]<br/>";
        } else {
            if($path) {
                echo $path."[".$key."]<br/>";
            }
            print_keys_recursive($value, $path."[".$key."]");
        }
    }
    return;
}
print_keys_recursive($arr);

Output:

[key][subkey]
[key][another_subkey]
[key][another_subkey][subkey_under_subkey]
[key][yet_another_subkey]
[another_key][etc]
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure how you want the output since you have not provided an example array, just the result, but here is an example based on the following array,

$array = array(
    "key" => array(
        "subkey" => 1, 
        "another_subkey" => array("2", "subkey_under_subkey" => 3), 
        "yet_another_subkey" => 4
    ),
    "another_key" => array("etc"), 
    "last_key" => 0
); 

Using the following function,

function recursive_keys($arr, $history = NULL)
{
    foreach ($arr as $key => $value)
    {
        if (is_array($value))
            recursive_keys($value, $history."['".$key."']");
        else
            echo $history."['".$key."']\n";
    }
}

Output of recursive_keys($array) is,

['key']['subkey']
['key']['another_subkey']['0']
['key']['another_subkey']['subkey_under_subkey']
['key']['yet_another_subkey']
['another_key']['0']
['last_key']

Comments

0

Try this

function arrayMultiKeys($array,$depth = 0){
    foreach($array as $k=>$v){
        echo "['".$k."']";
        if(is_array($v)){
            arrayMultiKeys($v,$depth + 1);
        }
        if($depth == 0 ){
            echo "<br>";
        }
    }
}

1 Comment

This function has a problem at the second level similar to mine, although it seems a lot cleaner in approach. A second second level key under the same first level key gets outputed as a third level key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.