具有挑战性的问题:PHP试图获得一个不错的数组

时间:2014-04-10 10:05:58

标签: php arrays

我正在努力解决这个问题:

我用这个:

 $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
 foreach ($objects as $name => $object) {
    echo $name . '<br/>';
 }

结果:

./index.php
./test
./test/_test
./test/_test/__test
./.htaccess
./memoire
./memoire/EPWORTH.pdf
./memoire/consentement.pdf
./memoire/DIRECT.pdf
./memoire/observatoire.pdf
./memoire/HAD.pdf
./memoire/COPD.pdf
./memoire/MRC.pdf
./categorie
./categorie/seminaire
./categorie/seminaire/module1
./categorie/seminaire/module1/documents
./categorie/seminaire/module1/documents/04_HelloWorld.pdf
./categorie/seminaire/module1/information
./categorie/seminaire/module1/information/VSRQ.pdf
./categorie/seminaire/module1/presentation
./categorie/seminaire/module1/presentation/VQ11.pdf
./categorie/seminaire/module2
./categorie/seminaire/module2/information
./categorie/seminaire/module2/information/MRC.pdf
./categorie/seminaire/module2/présentation
./categorie/seminaire/module2/présentation/COPD.pdf
./categorie/seminaire/module2/documents
./categorie/seminaire/module2/documents/HAD.pdf

我想要获得的是一个很好的数组:

$result = array(
    "memoire" => array(
        "type" => "folder",
        "subs" => array(
            "consentement.pdf" => array("type" => "file")
        )
    ),
    "categorie" => array(
        "type" => "folder",
        "subs" => array(
            "seminaire" =>
            array(
                "type" => "folder",
                "subs" => array(
                    "module1" => array(
                        "type" => "folder",
                        "subs" => array(
                            "documents" => array(
                                "type" => "folder",
                                "subs" => array("04_HelloWorld.pdf" => array("type" => "file"))
                            )
                        )
                    )
                )
            )
        )
    )
);

1 个答案:

答案 0 :(得分:0)

感谢@Prasanth和http://kvz.io/,这就是我所做的:

function arrayToTree($array, $delimiter = '/', $type = true) {
    if (!is_array($array))
        return false;
    $splitRE = '/' . preg_quote($delimiter, '/') . '/';
    $returnArr = array();
    foreach ($array as $key => $val) {
        // Get parent parts and the current leaf
        $parts = preg_split($splitRE, $key, -1, PREG_SPLIT_NO_EMPTY);
        $leafPart = array_pop($parts);

        // Build parent structure
        // Might be slow for really deep and large structures
        $parentArr = &$returnArr;
        foreach ($parts as $part) {
            if (!isset($parentArr[$part])) {
                $parentArr[$part] = array();
            } elseif (!is_array($parentArr[$part])) {
                if ($type) {

                    $parentArr[$part] = array('___type' => (is_dir($val)) ? 'folder' : 'file', '__base' => $parentArr[$part]);
                } else {
                    $parentArr[$part] = array();
                }
            }
            $parentArr = &$parentArr[$part];
        }

        // Add the final part to the structure
        if (empty($parentArr[$leafPart])) {
            $parentArr[$leafPart] = array('___type' => (is_dir($val)) ? 'folder' : 'file', '__base' => $val);
        } elseif ($type && is_array($parentArr[$leafPart])) {
            $parentArr[$leafPart]['type'] = (is_dir($val)) ? 'folder' : 'file';
        }
    }
    return $returnArr;
}