4

I need to extract a associative array keys into a string and implode with "/" or any character/symbols.

For eg:

$array = array([key1] => 
                array([key11] => 
                     array([key111] => 'value111', 
                           [key112] => 'value112', 
                           [key113] => 'value113',
                          ),
                 ),
           );

I need an output as below array:

array([0] => 'key1/key11/key111',[1] => 'key1/key11/key112', [2] => 'key1/key11/key112');

3 Answers 3

2

I've edited an answer given here and came up with the following code.

function listArrayRecursive($someArray, &$outputArray, $separator = "/") {
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST);
    foreach ($iterator as $k => $v) {

        if (!$iterator->hasChildren()) {
            for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
                $p[] = $iterator->getSubIterator($i)->key();
            }
            $path = implode($separator, $p);
            $outputArray[] = $path;
        }
    }
}

$outputArray = array();
listArrayRecursive($array, $outputArray);
print_r($outputArray);

Input:

Array
(
    [key1] => Array
        (
            [key11] => Array
                (
                    [key111] => value111
                    [key112] => value113
                    [key113] => value113
                )
        )
)

Output:

Array
(
    [0] => key1/key11/key111
    [1] => key1/key11/key112
    [2] => key1/key11/key113
)
Sign up to request clarification or add additional context in comments.

4 Comments

Why deleted your comment related to the implode line which I've never complained and gave me a downvote for that!? Strange kind of discussion here.
I've deleted the comment because I saw your first version was the same as my code, so you were right. And who said I've downvoted you?
Huh? My first version? The first version of my answer was same as yours? I've made my answer just 12 minutes before yours. And I can't understand your first sentence, because your comment was My answer has this line $path = implode('/', $p); which defines the operator and what's this related to my first version? I don't mind either way. Discussion is finished.
@rabude I'm having difficulties to explain myself. I meant in my comment that my answer let you change the separator (inside the function) and your first version did not, hence the poster's comment on your answer. But then I saw your first version also allows to change the separator, so I've deleted my comment.
1

Works for different depth of array:

function getKeys($array, $prefix='', $separator = '/') {
  $return = array();
  foreach($array as $key => $value) {
    if (!is_array($value)) $return[] = $prefix . $key;
    else $return = array_merge($return, getKeys($value, $prefix . $key . separator), $separator);
  }
  return $return;
}

$keys = getKeys($array, '', '#');

See online fiddle http://ideone.com/krU4Xn

3 Comments

Its works, But how can i change that "/" into dynamic way as using function param.
See my edit (fiddle is also modified), but this should be an easy one, if you read the manual on php.net/manual/en/language.functions.php
And why do you accept an other answer, that seems to be more incomprehensible for you than mine and also has no option to specify separator!?
0

you could do something like...

    $mapArray = array();
    $symbol = '/';
    foreach($array as $k =>$v)
        foreach($v as $k1 =>$v1)
            foreach($v1 as $k2 =>$v2)
                $mapArray[] = $k.$symbol.$k1.$symbol.$k2; 

also this obviously only works in this particular case, if it needs to be more generic it can be done, but I think this should get you started.

Comments

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.