4

I get stuck with recursion functions and I couldn't find anything that would quite do what I need, so I'm hoping someone can help me with this. I've got a multidimensional array of directories for a given drive and I need to convert them into full path strings for EACH deepest subdirectory. Here's the array:

<?php
$array = array(
'CANON' => array(
    'DCIM' => array(
        '100CANON',
        '101CANON',
        'CANONMSC'
    ),
    'CANON_SC' => array(
        'IMAGE' => array(
            '0001'
        ),
        'DOCUMENT' => array(
            '0001'
        ),
    ),
    'MISC'
));

Here's the desired output:

/CANON/DCIM/100CANON/
/CANON/DCIM/101CANON/
/CANON/DCIM/CANONMSC/
/CANON/CANON_SC/IMAGE/0001/
/CANON/CANON_SC/DOCUMENT/0001/
/CANON/MISC/

This is what I have so far, but it only works for the first deepest directory and ignores the sibling directories.

<?php
function flatten_directory($array, $prefix = '')
{
    $result = '';

    foreach($array as $key=>$value)
    {
        if(is_array($value))
        {
            $result .= $key .'/'. flatten_directory($value, $prefix . $key . '.');
        }
        else
        {
            $result .= $prefix . $key . $value;
        }
    }
    return $result;
}

Thank you in advance.

1
  • Why do you put a '.' in the prefix when you find that the value is array ? Commented Mar 9, 2016 at 14:00

2 Answers 2

2

The prefix idea is good but this part doesn't work because you might get multiple results:

$result .= $key .'/'. flatten_directory($value, $prefix . $key . '.');

Rather than returning a single string, return an array of strings. You're also mixing up when to use $key and $value.

function flatten_directory($directory, $prefix = "") {
    $result = array();
    foreach ($directory as $key => $part) {
        if (is_array($part)) {
            $result = array_merge($result, flatten_directory($part, $prefix . "/" . $key));
        } else {
            $result[] = $prefix . "/" . $part . "/";
        }
    }
    return $result;
}

Output:

Array
(
    [0] => /CANON/DCIM/100CANON/
    [1] => /CANON/DCIM/101CANON/
    [2] => /CANON/DCIM/CANONMSC/
    [3] => /CANON/CANON_SC/IMAGE/0001/
    [4] => /CANON/CANON_SC/DOCUMENT/0001/
    [5] => /CANON/MISC/
)
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$array = array(
'CANON' => array(
    'DCIM' => array(
        '100CANON',
        '101CANON',
        'CANONMSC'
    ),
    'CANON_SC' => array(
        'IMAGE' => array(
            '0001'
        ),
        'DOCUMENT' => array(
            '0001'
        ),
    ),
    'MISC'
));

function arrayToString($array, $parentStr = '')
{
    foreach($array as $key => $token)
    {
        if($token && is_array($token))
        {
            arrayToString($token, $parentStr . '/' . $key);
        }
        else
        {
            echo $parentStr . '/' . $token . '<br>';
        }
    }
}

arrayToString($array);

?>

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.