merge分隔多维数组

时间:2015-07-02 16:12:45

标签: php

当我print_r($_FILES);时,结果是

Array
(
    [one] => Array
        (
            [name] => Array
                (
                    [0] => one1.jpg
                    [1] => one2.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp\tmp\php81D9.tmp
                    [1] => C:\xampp\tmp\php81F9.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 129859
                    [1] => 134437
                )

        )

    [two] => Array
        (
            [name] => Array
                (
                    [0] => two1.jpg
                    [1] => two2.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp\tmp\php81FA.tmp
                    [1] => C:\xampp\tmp\php820A.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 138108
                    [1] => 142492
                )

        )

)   

当我这样做时:

    foreach ($_FILES as $color => $value)
        {
            foreach ($value['name'] as $name)
                {
                    $named['name'] = $name;
                    $nameds[] = $named ;
                } 
        }
    echo "<pre>";
    print_r($nameds);

结果是:

Array
(
    [0] => Array
        (
            [name] => one1.jpg
        )

    [1] => Array
        (
            [name] => one2.jpg
        )

    [2] => Array
        (
            [name] => two1.jpg
        )

    [3] => Array
        (
            [name] => two2jpg
        )

)

^这是我想要实现的预期结果。但是那里没有插入其余的密钥(只有name)。我希望所有的密钥都在typetmp_name等等。

因此,最终结果将是:

Array
(
    [0] => Array
        (
            [name] => one1.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php81D9.tmp
            [error] => 0
            [size] => 129859
        )
    [1] => Array
        (
            [name] => one2.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php81F9.tmp
            [error] => 0
            [size] => 134437
        )

    [2] => Array
        (
            [name] => two1.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php81FA.tmp
            [error] => 0
            [size] => 138108
        )

    [3] => Array
        (
            [name] => two2jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php820A.tmp
            [error] => 0
            [size] => 142492
        )

)

我想知道是否有人能告诉我这是最简单的方法。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

<?php

$_FILES = Array
(
    "one" => Array
        (
            "name" => Array
                (
                    "0" => "one1.jpg",
                    "1" => "one2.jpg"
                ),

            "type" => Array
                (
                    "0" => "image/jpeg",
                    "1" => "image/jpeg"
                ),

            "tmp_name" => Array
                (
                    "0" => "C:\\xampp\\tmp\\php81D9.tmp",
                    "1" => "C:\\xampp\\tmp\\php81F9.tmp"
                ),

            "error" => Array
                (
                    "0" => "0",
                    "1" => "0"
                ),

            "size" => Array
                (
                    "0" => "129859",
                    "1" => "134437"
                )

        ),
    "two" => Array
        (
            "name" => Array
                (
                    "0" => "two1.jpg",
                    "1" => "two2.jpg"
                ),

            "type" => Array
                (
                    "0" => "image/jpeg",
                    "1" => "image/jpeg"
                ),

            "tmp_name" => Array
                (
                    "0" => "C:\\xampp\\tmp\\php81FA.tmp",
                    "1" => "C:\\xampp\\tmp\\php820A.tmp"
                ),

            "error" => Array
                (
                    "0" => "0",
                    "1" => "0"
                ),

            "size" => Array
                (
                    "0" => "138108",
                    "1" => "142492"
                )

        )

);   


$counter = 0;

foreach ($_FILES AS $value) {

    $nbr = count($value['name']) - 1;

    for($x = 0; $x <= $nbr; $x++){
        foreach ($value AS $key => $elements) {           
            $newArray[$counter][$key]  = $elements[$x];  
        }         
        $counter++;
    }
}

echo "<pre>";
print_r($newArray);
?>