Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CSV文件到具有物化路径的平面阵列_Php - Fatal编程技术网

Php CSV文件到具有物化路径的平面阵列

Php CSV文件到具有物化路径的平面阵列,php,Php,我有一个包含文件和目录列表的CSV文件: Depth;Directory; 0;bin 1;basename 1;bash 1;cat 1;cgclassify 1;cgcreate 0;etc 1;aliases 1;audit 2;auditd.conf 2;audit.rules 0;home .... 每条线取决于上面的一条(对于深度参数) 我想创建一个这样的数组,以便将其存储到我的 我不知道如何处理。。。 有什么想法吗 编辑1: 我并没有真正使用目录,这只是一个例子,所以我不能使用文

我有一个包含文件和目录列表的CSV文件:

Depth;Directory;
0;bin
1;basename
1;bash
1;cat
1;cgclassify
1;cgcreate
0;etc
1;aliases
1;audit
2;auditd.conf
2;audit.rules
0;home
....
每条线取决于上面的一条(对于深度参数)

我想创建一个这样的数组,以便将其存储到我的

我不知道如何处理。。。 有什么想法吗

编辑1: 我并没有真正使用目录,这只是一个例子,所以我不能使用文件系统函数或文件迭代器

编辑2: 通过这个CSV文件,我可以创建一个JSON嵌套数组:

function nestedarray($row){
    list($id, $depth, $cmd) = $row;

    $arr = &$tree_map;

        while($depth--) {
         end($arr ); 
         $arr = &$arr [key($arr )];
    }

    $arr [$cmd] = null;             

}
但我不确定这是否是最好的方法……“最好”的方法是不要将数据存储为CSV格式,因为这是错误的工作工具

也就是说,给你:

<?php
$lines = file('/path/to/your/csv_file.csv');
$directories = array();
$path = array();
$lastDepth = NULL;

foreach ($lines as $line) {
    list($depth, $dir) = str_getcsv($line, ';');

    // Skip headers and such
    if (!ctype_digit($depth)) {
        continue;
    }

    if ($depth == $lastDepth) {
        // If this depth is the same as the last, pop the last directory
        // we added off the stack
        array_pop($path);
    } else if ($depth == 0) {
        // At depth 0, reset the path
        $path = array();
    }

    // Push the current directory onto the path stack
    $path[] = $dir;

    $directories[] = array(
        '_id' => NULL,
        'name' => $dir,
        'path' => implode(',', $path)
    );

    $lastDepth = $depth;
}

var_dump($directories);

我认为这应该可以做到(至少在我的测试中,它对您的数据有效)。请注意,此代码不会执行太多错误检查,并希望输入数据的顺序正确(即从级别0开始,没有孔)



我首先尝试构建一个类似{dir1:[dir11:[file111,file112],dir12:[…]}的嵌套数组,然后应用递归函数,但我无法成功。数组已经被展平。您的示例也非常具体。深度是否达到0,1,2,1?这会使事情变得复杂。是的,数组被展平了(我没有编辑我的标题)。是的,深度可以更深入。。更深入:)谢谢你的回答,真的,但它没有按预期工作:-(它怎么没有按预期工作?如果你把你在问题中输入的内容输入它,它提供的输出与你说的你想要的相同。
<?php
$lines = file('/path/to/your/csv_file.csv');
$directories = array();
$path = array();
$lastDepth = NULL;

foreach ($lines as $line) {
    list($depth, $dir) = str_getcsv($line, ';');

    // Skip headers and such
    if (!ctype_digit($depth)) {
        continue;
    }

    if ($depth == $lastDepth) {
        // If this depth is the same as the last, pop the last directory
        // we added off the stack
        array_pop($path);
    } else if ($depth == 0) {
        // At depth 0, reset the path
        $path = array();
    }

    // Push the current directory onto the path stack
    $path[] = $dir;

    $directories[] = array(
        '_id' => NULL,
        'name' => $dir,
        'path' => implode(',', $path)
    );

    $lastDepth = $depth;
}

var_dump($directories);
<?php

$input = explode("\n",file_get_contents($argv[1]));
array_shift($input);

$data = array();
foreach($input as $dir)
{
    if(count($parts = str_getcsv($dir, ';')) < 2)
    {
        continue;
    }

    if($parts[0] == 0)
    {
        $last = array('_id' => null,
                      'name' => $parts[1],
                      'path' => $parts[1]);
        $levels = array($last);
        $data[] = $last;
    }
    else
    {
        $last = array('id' => null,
                      'name' => $parts[1],
                      'path' => $levels[$parts[0] - 1]['path'] . ',' . $parts[1]);
        $levels[$parts[0]] = $last;
        $data[] = $last;
    }
}

print_r($data);

?>