-1

我正在尝试从各种路径生成维度数组。为此,我正在使用此处找到的功能。

我的代码

function get_dir_content_to_array( string $dir_path, string $dir_filter = null, string $file_filter = null ) {
    if( is_dir_empty( $dir_path ) )
        return false;
    $output = array();
    $files = get_subdir_filtered( $dir_path, $dir_filter, $file_filter );
    if ( isset( $files ) ) {
        foreach ( $files as $name => $object ) {
            if ( $object->getFilename() !== "." && $object->getFilename() !== ".." ) {
                // Split by the delimiter.
                $delimiter       = "/";
                $name            = str_replace( "\\", $delimiter, $name );
                $relative_path   = str_replace( $dir_path, "", $name );
                $a_relative_path = explode( $delimiter, $relative_path );
                $path = [ array_pop( $a_relative_path ) ];
                foreach ( array_reverse( $a_relative_path ) as $pathPart ) {
                    $path = [ $pathPart => $path ];
                }

                // Add it to a temp list.
                $paths[] = $path;
            }
            $output = call_user_func_array( 'array_merge_recursive', $paths );

        }
    }
    return $output;
}

function get_subdir_filtered( $dir_path, $dir_filter, $file_filter ) {
    $path      = realpath( $dir_path );
    $directory = new \RecursiveDirectoryIterator( $path );
    $files = null;
    if ( ! empty( $dir_filter ) || ! empty( $file_filter ) ) {
        if ( ! empty( $dir_filter ) ) {
            $filter = new DirnameFilter( $directory, $dir_filter );
        }
        if ( ! empty( $file_filter ) ) {
            $filter = new FilenameFilter( $filter, $file_filter );
        }
        $files = new \RecursiveIteratorIterator( $filter );
    } else {
        $files = new \RecursiveIteratorIterator( $directory );
    }
    return $files;
}

class DirnameFilter extends FilesystemRegexFilter {
    // Filter directories against the regex
    public function accept() {
        return ( ! $this->isDir() || preg_match( $this->regex, $this->getFilename() ) );
    }
}

这有效,除非文件夹被命名为“0”

我该如何解决?为什么 array_pop 跳过值“0”,即使它是一个字符串?

4

2 回答 2

2

json_encode()对一个键是从 开始的顺序整数0或它们的等效字符串进行编码时,它会生成 JSON 数组而不是对象。

您可以使用该JSON_FORCE_OBJECT标志,但这会将文件夹内的文件名数组转换为您不想要的对象。

您可以做的是使用 PHP 对象而不是数组来表示文件夹。json_encode()将其编码为一个对象,即使它具有数字属性。

我认为这可能会做到:

function get_dir_content_to_array( string $dir_path, string $dir_filter = null, string $file_filter = null ) {
    if( is_dir_empty( $dir_path ) )
        return false;
    $output = array();
    $files = get_subdir_filtered( $dir_path, $dir_filter, $file_filter );
    if ( isset( $files ) ) {
        foreach ( $files as $name => $object ) {
            if ( $object->getFilename() !== "." && $object->getFilename() !== ".." ) {
                // Split by the delimiter.
                $delimiter       = "/";
                $name            = str_replace( "\\", $delimiter, $name );
                $relative_path   = str_replace( $dir_path, "", $name );
                $a_relative_path = explode( $delimiter, $relative_path );
                $path = [ array_pop( $a_relative_path ) ];
                foreach ( array_reverse( $a_relative_path ) as $pathPart ) {
                    $folder = new StdClass;
                    $folder->{$pathPart} = $path;
                    $path = $folder;
                }

                // Add it to a temp list.
                $paths[] = $path;
            }
            $output = call_user_func_array( 'array_merge_recursive', $paths);

        }
    }
    return $output;
}

我没有测试它,因为我没有这个get_subdir_filtered()功能。这可能array_merge_recursive不会正确合并。您可能还需要合并对象。本教程包含一个mergeObjectsRecursively我认为应该是有用的替代品的实现。

于 2018-12-14T23:52:19.090 回答
0

就我而言,我得到了这条路: update.json and 0/1/0/filename.zip

正如@Barmar 所说,基本上,问题出在我尝试将数组转换为 json 对象时。

在 PHP 中,数组实际上一直是一个对象,所以... $arr = [ "0" => [ "1" => ... ] ] 对于 php,等于:$arr = [ 0 => [1 => ...]]。在 PHP 7.2.x 中,每次合并对象时,"0"as 字符串键将被强制转换0为 int。

结果

1. 使用 json_encode

echo json_encode( get_dir_content_to_array(...) );

//result = ["update.json",{"1":[["filename.zip"]]}]

2. 使用 json_encode 和JSON_FORCE_OBJECT

echo json_encode( get_dir_content_to_array(...), JSON_FORCE_OBJECT );

//result = {"0":"update.json","1":{"1":{"0":{"0":"filename.zip"}}}}
//I got 1,0,0,filename.zip instead of 0,1,0,filename.zip 

3. 添加(字符串)

 $path = [array_pop( $a_relative_path)];
 foreach ( array_reverse( $a_relative_path ) as $pathPart ) {
    $path = [ (string) $pathPart => $path ];
 }
 //Same result of 2.

为了保持路径的正确顺序,我暂时找到了一个 hacky 解决方案:

在每个键前添加下划线

foreach ( array_reverse(  $a_relative_path ) as $pathPart ) {
    $path = [ "_" .  $a_relative_path => $path ];
}
//result = {"0":"update.json", "_0":{"_1":{"_0":{"0":"filenamezip"}}}} 

//With a new file path, I got this :
// {"0":"update.json","_toto":{"_foo":{"0":"test.txt"}},"_0":{"_1":{"_0":{"0":"filename.zip"}}}}

这个解决方案是一个 hack,但我可以区分作为路径的 kee"_0":{"_1":{"_0"和索引文件的键 "0":"filename.zip"

于 2018-12-15T00:53:28.603 回答