尝试从String创建数组(文件/文件夹结构)

时间:2012-05-27 10:52:08

标签: php arrays recursion

我有这个字符串:

css/
reset.css
style.css
img/
js/
lib/
    jquery.js
script.js
index.php

我想要那个数组:

Array
(
    [js/] => Array
        (
            [lib/] => Array
                (
                    [jquery.js] => 
                )
            [script.js] => 
        )
    [index.php] => 
)

我写了这段代码:

class Structurer{
    public function parse(){
        $output = array();
        $level = 0;
        $currentFolder = '';

        function throughtLevels(&$arr, $key, $newKey, $newValue){
            if(is_array($arr)){
                foreach($arr as $k => $v){
                    if($k == $key){echo 'ok';
                        if(is_array($arr[$k]))  $arr[$k] = array_merge($arr[$k], array($newKey => $newValue));
                        else    $arr[$k] = array($newKey => $newValue);
                    }
                    else    throughtLevels($arr[$k], $key, $newKey, $newValue);
                }
            }
        } // throughtLevels
        while($this->moveToNextLine()){
            $curl = '';

            if(preg_match_all('#\t#', $this->currentLine, $n)){
                $this->currentLine = str_replace("\t", '', $this->currentLine);
                $level = count($n[0]);
            }
            else $level = 0;

            if(preg_match_all('#\[(.*)\]#', $this->currentLine, $com)){
                $this->currentLine = str_replace($com[0], '', $this->currentLine);
                $curl = implode('', $com[1]);
            }

            if(preg_match('#\/#', $this->currentLine)){
                if($level > 0)  throughtLevels($output, $currentFolder, $this->currentLine, array());
                else    $output[$this->currentLine] = array();

                $currentFolder = $this->currentLine;
            } else {
                if(!empty($this->currentLine)){
                    if($level > 0)throughtLevels($output, $currentFolder, $this->currentLine, $curl);
                    else    $output[$this->currentLine] = $curl;
                }
            }
        }

        echo '<pre>' . print_r($output, 1) . '</pre>';
    } // parse(value)

    private function moveToNextLine(){
        if($this->currentLineNum >= count($this->lines) -1) return false;
        $this->currentLine = $this->lines[++$this->currentLineNum];
        return true;
    } // moveTonextLine


    private static function cleanup($value){
        $value = str_replace(array("\r\n", "\r"), "\n", $value);
        $value = preg_replace('#/\*[^*]*\*+([^/][^*]*\*+)*/#', '', $value);
        $value = preg_replace('#^\n#', '', $value);

        return $value;
    } // cleanup(value)


    public function __construct($value){
        $this->base = dirname(__FILE__);
        $this->currentLineNb = -1;
        $this->currentLine = '';
        $this->lines = explode("\n", self::cleanup($value));

        $this->parse();
    } // __construct(value)


    private $lines = array();
    private $currentLineNum = -1;
    private $currentLine = '';
} // Structurer
$input = <<<EOT
css/
    reset.css
    style.css
img/
js/
    lib/
        jquery.js
    script.js
index.php
EOT;
$arr = new Structurer($input);

并生成该数组:

Array
(
    [js/] => Array
        (
            [lib/] => Array
                (
                    [jquery.js] => 
                    [script.js] => 
                )
        )
    [index.php] => 
)

我真的不知道如何解决这个问题。我想我不远,但我不能前进。

1 个答案:

答案 0 :(得分:1)

我建议你看看RecursiveDirectoryIterator

要线性迭代,请使用:

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $entry) {
    // do stuff with $entry
}