PHP文件列表数组的数组树 [英] php file list array into array tree

查看:179
本文介绍了PHP文件列表数组的数组树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有超过数百号元素的一些文件列表数组,我需要转换为树阵,并将其插入到数据库中并调用返回的树一个JSON API目录系统
我花了一个星期尝试不同的算法,它们都不能在某一时刻。
这是阵列

I have some file list arrays with elements exceeding 100s in number that I need to convert to a tree array and insert them into the database and return the tree on call for a json api directory system I have spent a week trying different algorithms, all of them fail at some point. This is the array:

$files[] = array('dir1/dir2/dir3/file1.mkv', 44444);
$files[] = array('dir1/dir2/dir3/file2.mkv', 44444);
$files[] = array('dir1/dir2/file1.mkv', 44444);
$files[] = array('dir1/dir2/file2.txt', 11111);
$files[] = array('dir1/file1.exe', 22222);
$files[] = array('dir1/file2.exe', 22222);
$files[] = array('file1.rar', 3333);

第一个元素是文件路径,第二个元素是文件大小。
这些文件不此服务器上存在的,它们是在不同的服务器上,这是API服务器。
我需要将其转换成一个嵌套的树阵是这样的:

the first element is the file path, second element is filesize. These files dont exist on this server, they are on a different server, this is the api server. I need to convert it into a nested tree array like this:

Array
(
    [dir1] => Array
        (
            [dir2] => Array
                (
                    [dir3] => Array
                    (
                        [0] => Array
                            (
                                [name] => file1.mkv
                                [size] => 44444
                            )

                        [1] => Array
                            (
                                [name] => file2.mkv
                                [size] => 44444
                            )

                    )

                    [0] => Array
                        (
                            [name] => file1.mkv
                            [size] => 44444
                        )

                    [1] => Array
                        (
                            [name] => file2.txt
                            [size] => 11111
                        )

                )

            [0] => Array
                (
                    [name] => file1.exe
                    [size] => 22222
                )

            [1] => Array
                (
                    [name] => file2.exe
                    [size] => 22222
                )

        )

    [0] => Array
        (
            [name] => file1.rar
            [size] => 3333
        )

)

我写了一些code,但它只是上升到第2级(DIR2),然后在第三级(DIR3),它未能达到预期效果。

I wrote some code but it only goes up to 2nd level (dir2), then on the third level (dir3), it fails to perform as expected.

在code:

foreach ($files as $file)
{
    $info = pathinfo($file[0]);
    if ($info['dirname'] != '.')
    {
        if (strpos($info['dirname'], '/') !== false)
        {
            $dirs[pathinfo($info['dirname'])['dirname']][pathinfo($info['dirname'])['basename']][] = array('name' => $info['basename'], 'size' => $file[1]);
        }
        else
        {
            $dirs[$info['dirname']][] = array('name' => $info['basename'], 'size' => $file[1]);
        }
    }
    else
    {
        $dirs[] = array('name' => $info['basename'], 'size' => $file[1]);
    }
}

这code返回该数组的罚款,如果我从文件中删除列表阵列DIR3水平,但与DIR3 present,它提供了:

This code returns the array fine if I remove the dir3 level from the file list array, but with the dir3 present, it gives:

Array
(
    [dir1/dir2] => Array
        (
            [dir3] => Array
                (
                    [0] => Array
                        (
                            [name] => file1.mkv
                            [size] => 44444
                        )

                    [1] => Array
                        (
                            [name] => file2.mkv
                            [size] => 44444
                        )

                )

        )

    [dir1] => Array
        (
            [dir2] => Array
                (
                    [0] => Array
                        (
                            [name] => file1.mkv
                            [size] => 44444
                        )

                    [1] => Array
                        (
                            [name] => file2.txt
                            [size] => 11111
                        )

                )

            [0] => Array
                (
                    [name] => file1.exe
                    [size] => 22222
                )

            [1] => Array
                (
                    [name] => file2.exe
                    [size] => 22222
                )

        )

    [0] => Array
        (
            [name] => file1.rar
            [size] => 3333
        )

)

这[DIR1 / DIR2]的问题,我需要它是一个完美的树,所以我可以通过它再次出现,插入记录到数据库中。

This [dir1/dir2] is the problem, I need it to be a perfect tree so I can recurs through it and insert the records into the database.

推荐答案

既然你不知道树的深度事前,你不能用的if / else循环进行管理。这是一个需要递归一个用例。

Since you don't know tree depth beforehand, you can't manage this with if/else loops. It is a use case that needs recursion.

<?php
function scanpath($path) {
    $myscan = scandir($path);
    $tree=[];
    foreach($myscan as $entry) {
        //echo '<br>'.$entry;
        if($entry==='.' || $entry ==='..') {
            // do nothing
        } else  if(is_dir($path.'/'.$entry)) {
            // this is a folder, I will recurse
            $tree[$entry] = scanpath($path.'/'.$entry);
        } else {
            // this is a file or link. Value is file size
            $tree[$entry] = filesize($path.'/'.$entry);
        }
    }
    return $tree;
}
$scanresult=scanpath(__DIR__);
echo '<pre>';
print_r($scanresult);
echo '</pre>';

您看,我调用一个函数在给定的道路。对于路径上的每个条目我

You see, I call a function on a given path. For each entry on that path I


  • 放弃。和..

  • 如果它是一个文件夹,该节点将包含应用该功能的子路径的结果

  • 在任何其他情况下,该节点将包含文件的大小

您将需要修改我的code,以满足您的需求。我可以告诉你的是文件或链接的情况有不同的格式。

You'll need to modify my code to suit your needs. I can tell your "is file or link" case has a different format.

修改:因为没有一个真正的文件结构进行扫描,而是你有路径的数组,这是我来了。

EDIT: since there isn't a real file structure to scan, but instead you have an array of paths, this is the solution I came with

<?php
$thefiles=[];

$thefiles[] = array('dir1/dir2/dir3/file1.mkv', 44444);
$thefiles[] = array('dir1/dir2/dir3/file2.mkv', 44444);
$thefiles[] = array('dir1/dir2/file1.mkv', 44444);
$thefiles[] = array('dir1/dir2/file2.txt', 11111);
$thefiles[] = array('dir1/dir4/file5.mkv', 22444);
$thefiles[] = array('dir1/dir4/file6.txt', 15111);
$thefiles[] = array('dir1/file1.exe', 22222);
$thefiles[] = array('dir1/file2.exe', 22222);
$thefiles[] = array('file1.rar', 3333);

$filearray=[];

function scanpath($patharray,$filesize) {
    $tree=[];
    if(count($patharray)===1) {
        $filename=array_pop($patharray);
        $tree[] = ['name'=>$filename, 'size'=>$filesize];
    } else {
        $pathpart = array_pop($patharray);
        $tree[$pathpart] = scanpath($patharray,$filesize);
    }
    return $tree;
}

foreach($thefiles as $fileentry) {
    $patharray=array_reverse(explode('/',$fileentry[0]));
    $thisarray = scanpath($patharray,$fileentry[1]);
    $filearray= array_merge_recursive($filearray,$thisarray);
}

echo '<pre>';
print_r($filearray);
echo '</pre>';

我使用爆炸斜线作为分隔每个路径,然后通过递归的文件夹的路径,文件夹,直到我得到的最后部分和文件名,大小递归结束。

I exploded each path using slash as separator, then recursed on the path, folder by folder, until I get to the final part and the recursion ends on a file name and size.

这篇关于PHP文件列表数组的数组树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆