0

Example Array:

$array = array([key1] => 
                array([key11] => 
                     array([key111] => 'value111', 
                           [key112] => 'value112', 
                           [key113] => 'value113',
                           [key114] => array(A,B,C,D), 
                          ),
                 ),
           );

I need an output as below array:

array([key1/key11/key111] => 'value111',
      [key1/key11/key112] => 'value112', 
      [key1/key11/key113] => 'value113',
      [key1/key11/key114] => 'A,B,C,D' );

and i have tried using this function,

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);

I cant able to find how to achieve this by using the above function for "key1/key11/key114" getting value as i expected. Please help me on this.

5
  • 1
    It seems there is a special case in your output that won't make any solution straightforward. You want key1/key11/key114 to be A,B,C,D, when the obvious recursive output would be key1/key11/key114/0 = A, key1/key11/key114/1 = B, key1/key11/key114/2 = C, key1/key11/key114/3 = D. The output you desire will only happen if you have a limited recursion depth or something. Commented Nov 16, 2013 at 19:32
  • @Havenard, I need in this way only, if please suggest me another recursive function to do me a job as i expected. Commented Nov 16, 2013 at 19:35
  • @Lorenz Meyer What was the complicated you saying in this? Commented Nov 16, 2013 at 19:37
  • It was this that made me say complicated : $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST); Commented Nov 16, 2013 at 19:56
  • Using recursive iterators on problems with recursion shouldn't be complicated - in fact the RecursiveIteratorIterator unrolls all the recursiveness into a flat loop - that looks pretty easy to me. Commented Nov 16, 2013 at 21:43

2 Answers 2

3

Input:

$array = array(
    'key1' => array(
        'key11' => array(
            'key111' => 'value111', 
            'key112' => 'value112', 
            'key113' => 'value113',
            'key114' => array('A','B','C','D'), 
        ),
        'key12' => array(
            'key121' => 'value121', 
            'key122' => 'value122', 
            'key123' => 'value123',
            'key124' => array('A','B','C','D'), 
        ),
    ),
    'key2' => array(
        'key21' => array(
            'key211' => 'value111', 
            'key212' => 'value112', 
            'key213' => 'value113',
            'key214' => array('A','B','C','D'), 
        ),
    ),
);

Script:

function remap_keys($input, $max_depth, $separator = '/', /* reserved */ $keychain = array(), /* reserved */ &$output = array())
{
    foreach ($input as $key => $element)
    {
        $element_keychain = array_merge($keychain, (array)$key);
        if (($max_depth > 1) && is_array($element))
            remap_keys($element, $max_depth -1, $separator, $element_keychain, $output);
        else
            $output[implode($separator, $element_keychain)] = implode(',', (array)$element);
    }
    return $output;
}


$array = remap_keys($array, 3);

print_r($array);

Output:

Array
(
    [key1/key11/key111] => value111
    [key1/key11/key112] => value112
    [key1/key11/key113] => value113
    [key1/key11/key114] => A,B,C,D
    [key1/key12/key121] => value121
    [key1/key12/key122] => value122
    [key1/key12/key123] => value123
    [key1/key12/key124] => A,B,C,D
    [key2/key21/key211] => value111
    [key2/key21/key212] => value112
    [key2/key21/key213] => value113
    [key2/key21/key214] => A,B,C,D
)

http://ideone.com/pqH45h

Sign up to request clarification or add additional context in comments.

Comments

0
$array = array('key1' => 
                array('key11' => 
                     array('key111' => 'value111', 
                           'key112' => 'value112', 
                           'key113' => 'value113',
                           'key114' => array(A,B,C,D), 
                          ),
                 ),
           );
function implode_arr_keys($array, $output_arr = array(), $cur_key = FALSE) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            return implode_arr_keys($value, $output_arr, ($cur_key == FALSE ? $key : $cur_key.'/'.$key));
        } else {
            if(!is_numeric($key))
                $output_arr[$cur_key.'/'.$key] = $value;
            else
                $output_arr[$cur_key] = $array;
        }
    }
    return $output_arr;
}
print_r($array);
print_r(implode_arr_keys($array));

1 Comment

This works only if the first layers have only one element each.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.