Amereservant
Amereservant

Reputation: 807

PHP convert Multidimensional Array of directories into strings

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.

Upvotes: 4

Views: 112

Answers (2)

Halcyon
Halcyon

Reputation: 57721

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

Upvotes: 2

Nadir
Nadir

Reputation: 1819

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

?>

Upvotes: 1

Related Questions