从PHP另一个阵列构建一个多维数组动态? [英] Building a multidimensional array dynamically from another array with PHP?

查看:89
本文介绍了从PHP另一个阵列构建一个多维数组动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组中包含的是像这样的数据,

I have data contained in an array which is like so,

$file['info']['files'] = array(
    [0] => array(
         'length' => (int),
         'path' => array (
              [0] => 'file.txt',
         ),
    ),
    [1] => array(
         'length' => (int),
         'path' => array (
              [0] => 'directory one',
              [1] => 'file2.txt',
         ),
    ),
    [2] => array(
         'length' => (int),
         'path' => array (
              [0] => 'directory one',
              [1] => 'directory two',
              [2] => 'file3.txt',
         ),
    ),
);

$文件['信息'] ['文件'] 数组可以包含任意数量的元素。包含在每个路径阵列 $文件['信息'] ['文件'] 数组是我在哪里有麻烦了。

The $file['info']['files'] array can contain any number of elements. The path array contained in each $file['info']['files'] array is where I am having trouble.

它包含了有关文件结构的信息。如果仅1个元素存在,那么它是一个文件。如果一个以上的元素存在,那么每个单元从顶部开始是下一个元素的父文件夹和的最后一个元素是最近文件夹中的文件。以上面的例子将是一个文件结构

It contains information about a file structure. If just 1 element exists then it is a file. If more than one element exists then each element starting from the top is a parent folder of the next element and the last element is the file in the last folder. Taking the example above would be a file structure of

FILE file1.txt
FOLDER directory one
     FILE file2.txt
     FOLDER directory two
          FILE {file3.txt}

我想这个提取数据到我自己的阵列结构这是如下,

I would like to extract this data into my own array structure which is to be as follows,

 $sortedFiles = array(
     'file1.txt' => (int),
     'directory one' => array(
         'file2.txt' => (int),
         'directory two' => array(
              'file3.txt' => (int),
         ),
     ),
 );

我有这样的code,到目前为止,

I have this code so far,

foreach($file['info']['files'] as $file) {
    // LENGTH AND PATH ARE SET
    if(isset($file['length'], $file['path'])) {
        // GET COUNT OF FILE PATH ARRAY
        $count = count($file['path']);
        // SINGLE FILE
        if($count == 1) {
            $sortedFiles[$file['path'][0]] = $file['length'];
        // FILES IN DIRECTORY
        } else {
            // BUILD ARRAY STRUCTURE FOR DIRECTORIES
        }
    }
}

我有麻烦,当涉及到添加目录到阵列。我可以手动做到这一点,只能走这么多的目录,每次检查是否存在该目录的数组后,如果没有创造它,如果它存在,然后添加到它。我想这与下面的code,但只持续一个目录深(在code去的地方说 // BUILD阵列结构以上)。

// FOLDER NOT SET
if(!isset($files[$file['path'][0]])) {
    $sortedFiles[$file['path'][0]] = array($file['path'][1] => $file['length'],);
// FOLDER SET
} else {
    $sortedFiles[$file['path'][0]][$file['path'][1]] = $file['length'];
}

如何动态创建存在每个目录的阵列,并添加所需要铭记的目录结构可能是水平的任何数量的信息深?

How can I dynamically create arrays for each directory that exists and add the information that is needed bearing in mind that the directory structure could be any amount of levels deep?

感谢您抽出时间来阅读我的比较长的问题,我AP preciate任何帮助,任何人给我的。

Thanks for taking the time to read my rather long question and I appreciate any help that anyone gives to me.

推荐答案

您将需要递归调用你的函数,如下面的例子:

You will need to call your function recursively, as in the example below:

function get_contents_dir( $dir )
{
    $names = array();

    if ( is_dir($dir) && is_readable($dir) )
    {
            foreach ( scandir($dir) as $file )
            {
                    if ( is_dir($dir."/".$file) && is_readable($dir."/".$file) )
                    {
                            $names[] = get_contents_dir($dir."/".$file);
                    }

                    if ( is_file($dir."/".$file) && is_readable($dir."/".$file) )
                    {
                            $names[] = $dir."/".$file;
                    }
            }
    }

    return $names;
}

这个函数首先打开组 $ DIR 文件夹,扫描文件的列表,将每个发现的文件是数组,扫描的文件夹,<后code>收益 ED作为函数的返回值。

This function first opens the set $dir folder and scans the list of files, adding each found file to the array which is, after scanning the folder, returned as the return value of the function.

捻来的时候, SCANDIR()结果(文件夹中的文件和文件夹列表)的条目实际上是一个文件夹。如果出现这种情况,该函数是从它的内部调用,递归(见行 $名称[] = get_contents_dir($目录/$文件); 调用从函数内部函数)和子文件夹也将被编入索引。冲洗和重复,直到所有子文件夹进行索引。

The twist comes in when an entry of the scandir() result (list of files and folders in the folder) is actually a folder. If that happens, the function is called from it's internals, recursively (see the line $names[] = get_contents_dir($dir."/".$file); calling the function from within the function) and the subfolder will be indexed too. Rinse and repeat, until all subfolders are indexed.

如果你调用该函数并让它执行,数组将被返回。阵列的每个关键将是一个条目。如果它是一个文件,链接到关键字的值的文件的名称,如果它是一个文件夹,该值将是嵌套到previous彼此阵列

If you call the function and let it execute, an array will be returned. Each key of the array will be an entry. If it was a file, the value linked to the key is the name of the file, if it was a folder, the value will be another array nested into the previous one.

下面是采取了返回数组的例子转储:

Here is an example dump taken of the returned array:

array (
  0 => './libmysqlclient.so.16.0.0',
  1 => './libmysqlclient_r.so.16.0.0',
  2 => 
  array (
    0 => './libs/libboost_thread-mt.a',
    1 => './libs/libboost_thread-mt.so.1.38.0',
    2 => './libs/libmysql.dll',
    3 => './libs/libmysqlclient16_5.1.41-3ubuntu12_i386.deb',
  ),
  3 => 
  array (
    0 => './radio_sneaker/cl_auto.lua',
    1 => './radio_sneaker/sh_auto.lua',
    2 => './radio_sneaker/sh_coms.lua',
    3 => './radio_sneaker/sh_info.lua',
    4 => './radio_sneaker/sv_auto.lua',
    5 => './radio_sneaker/sv_hooks.lua',
  ),
  4 => './sv_auto.lua',
)

比较对命令在同一文件夹中跑出这样的输出:

Compare this output against the tree command ran on the same folder:

|   libmysqlclient.so.16.0.0
|   libmysqlclient_r.so.16.0.0
|   sv_auto.lua
|   
+---libs
|       libboost_thread-mt.a
|       libboost_thread-mt.so.1.38.0
|       libmysql.dll
|       libmysqlclient16_5.1.41-3ubuntu12_i386.deb
|       
\---radio_sneaker
        cl_auto.lua
        sh_auto.lua
        sh_coms.lua
        sh_info.lua
        sv_auto.lua
        sv_hooks.lua

这篇关于从PHP另一个阵列构建一个多维数组动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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