【问题标题】:How to store filename from a parsed array?如何从解析的数组中存储文件名?
【发布时间】:2014-11-20 11:12:17
【问题描述】:

我有一个名为 $result 的解析数组,我想将 $result 中的所有文件名存储到一个变量或另一个数组中?甚至使用 $result 只调用文件名。我想要文件名,因为我想从 ftp 服务器下载今天日期的所有 csv 文件。 这是我转储 $result 时的输出:

array(2) {
    [0]=> array(10) {
        ["isdir"]=> bool(false)
        ["perms"]=> string(14) "////-rw-r--r--"
        ["number"]=> string(1) "1"
        ["owner"]=> string(7) "4664210"
        ["group"]=> string(5) "15000"
        ["size"]=> string(5) "79360"
        ["month"]=> string(3) "Nov"
        ["day"]=> string(2) "20"
        ["time/year"]=> string(5) "03:55"
        ["name"]=> string(10) "order3.csv"
    }
    [1]=> array(10) {
        ["isdir"]=> bool(false)
        ["perms"]=> string(14) "////-rw-r--r--"
        ["number"]=> string(1) "1"
        ["owner"]=> string(7) "4664210"
        ["group"]=> string(5) "15000"
        ["size"]=> string(5) "79360"
        ["month"]=> string(3) "Nov"
        ["day"]=> string(2) "20"
        ["time/year"]=> string(5) "04:34"
        ["name"]=> string(10) "order4.csv" }
    }

我从这段代码中得到这个输出:

$result = parse_ftp_rawlist($files, false); // Function to parse array
var_dump ($result);

【问题讨论】:

  • I want to store all filenames from $result to a variable or to another array? 你问我们你想做什么?反正你为什么要创建另一个数组,为什么不直接用这个$result呢?

标签: php arrays parsing


【解决方案1】:

有人写的文档中有一个函数作为例子。您可以通过一些调整重新使用它。

function listDetailed($resource, $directory = '.')
{
    if (is_array($children = @ftp_rawlist($resource, $directory)))
    {
        $result = array();
        $items = array();

        foreach ($children as $child)
        {
            $chunks = preg_split("/\s+/", $child);
            list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time'],$item['name']) = $chunks;
            $item['type'] = $chunks[0]{0} === 'd' ? 'directory' : 'file';
            if($item['type'] == 'file')
            {
                $result['files'][] = $item['name'];
            }

        }
        return $result;
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2015-04-22
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多