如何将数组从一种格式转换为另一种格式?

时间:2014-07-26 12:22:59

标签: php arrays

我有这样的数组。

Array
(
    [0] => Array
        (
            [size] => 197602
            [files] => Array
                (
                    [0] => directory1
                    [1] => file1.jpg
                )

        )

    [1] => Array
        (
            [size] => 274893
            [files] => Array
                (
                    [0] => directory1
                    [1] => file2.jpg
                )

        )

    [2] => Array
        (
            [size] => 41
            [files] => Array
                (
                    [0] => directory2
                    [1] => file1.txt
                )

        )

    [3] => Array
        (
            [size] => 41
            [files] => Array
                (
                    [0] => directory2
                    [1] => file2.txt
                )

        )

    [4] => Array
        (
            [size] => 47
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file1.txt
                )

        )

    [5] => Array
        (
            [size] => 41
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file2.txt
                )

        )

    [6] => Array
        (
            [size] => 47
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file3.txt
                )

        )

    [7] => Array
        (
            [size] => 41
            [files] => Array
                (
                    [0] => file1.txt
                )

        )

    [8] => Array
        (
            [size] => 47
            [files] => Array
                (
                    [0] => file2.txt
                )

        )

)

我使用了此代码段。

<?php

foreach($array as $x)
{
    $path=implode('\\',$x['path']);
    $files[$path]=$x['length'];
}

并获得了上述数组的简化版本。

Array
(
    'directory1\file1.jpg' => 197602,
    'directory1\file2.jpg' => 274893,
    'directory2\file1.txt' => 41,
    'directory2\file2.txt' => 41,
    'directory2\sub-directory1\file1.txt' => 47,
    'directory2\sub-directory1\file2.txt' => 41,
    'directory2\sub-directory1\file3.txt' => 47,
    'file1.txt' => 41,
    'file2.txt' => 47
);

所以我的问题是如何从简化数组中获取原始数组,即第二个数组中的第一个数组。 ?

到目前为止,我已经尝试过了。

<?php

$array =    Array
    (
        'directory1\file1.jpg' => 197602,
        'directory1\file2.jpg' => 274893,
        'directory2\file1.txt' => 41,
        'directory2\file2.txt' => 41,
        'directory2\sub-directory1\file1.txt' => 47,
        'directory2\sub-directory1\file2.txt' => 41,
        'directory2\sub-directory1\file3.txt' => 47,
        'file1.txt' => 41,
        'file2.txt' => 47
    );

    print_r($array);


$new_array = array();

    foreach($array as $path => $size){

        if(strpos($path,'\\') !== false) {
        //file is under a directory tree/structure 

        $directory_tree = explode('\\',$path);

        $new_array[]['size'] = $size ;
        $new_array[]['files'] = $directory_tree;

        //last element in array is filename. 

       //print_r($directory_tree );

        }else{
        //its just file

        $new_array[]['size'] = $size;
        $new_array[]['path']= $path;

        }

   }


    print_r($new_array);

但获取大小和文件的双重数组。

Array
(
    [0] => Array
        (
            [size] => 197602
        )

    [1] => Array
        (
            [files] => Array
                (
                    [0] => directory1
                    [1] => file1.jpg
                )

        )

    [2] => Array
        (
            [size] => 274893
        )

    [3] => Array
        (
            [files] => Array
                (
                    [0] => directory1
                    [1] => file2.jpg
                )

        )

    [4] => Array
        (
            [size] => 41
        )

    [5] => Array
        (
            [files] => Array
                (
                    [0] => directory2
                    [1] => file1.txt
                )

        )

    [6] => Array
        (
            [size] => 41
        )

    [7] => Array
        (
            [files] => Array
                (
                    [0] => directory2
                    [1] => file2.txt
                )

        )

    [8] => Array
        (
            [size] => 47
        )

    [9] => Array
        (
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file1.txt
                )

        )

    [10] => Array
        (
            [size] => 41
        )

    [11] => Array
        (
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file2.txt
                )

        )

    [12] => Array
        (
            [size] => 47
        )

    [13] => Array
        (
            [files] => Array
                (
                    [0] => directory2
                    [1] => sub-directory1
                    [2] => file3.txt
                )

        )

    [14] => Array
        (
            [size] => 41
        )

    [15] => Array
        (
            [path] => file1.txt
        )

    [16] => Array
        (
            [size] => 47
        )

    [17] => Array
        (
            [path] => file2.txt
        )

)

我该如何解决这个问题?

感谢

2 个答案:

答案 0 :(得分:3)

您必须更改此代码段:

    $new_array[]['size'] = $size ;
    $new_array[]['files'] = $directory_tree;

到此:

    $new_array[] = array('size' => $size, 'files' => $directory_tree);

同样对于“正文”案例:

    $new_array[] = array('size' => $size, 'path' => $path);

答案 1 :(得分:0)

或者,您可以创建一个回调:

function parse($arr)
{
    return explode('\\', $arr);//process your array
}

然后在一行中完成:

$original = array_map('parse', $converted);