分组多维数组

时间:2013-08-30 09:53:34

标签: php arrays

所以我目前有一个看起来像这样的数组。

Array
(
    [name] => Array
        (
            [1] => Array
                (
                    [0] => 500aqua.jpg
                )

            [2] => Array
                (
                    [0] => 500beige.jpg
                    [1] => 500beige-zoom.jpg
                )

            [3] => Array
                (
                    [0] => P1040485.JPG
                    [1] => P1040486.JPG
                    [2] => P1040487.JPG
                )
        )

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

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

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

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

            [2] => Array
                (
                    [0] => C:\xampp\tmp\php5FA.tmp
                    [1] => C:\xampp\tmp\php5FB.tmp
                )

            [3] => Array
                (
                    [0] => C:\xampp\tmp\php60C.tmp
                    [1] => C:\xampp\tmp\php63B.tmp
                    [2] => C:\xampp\tmp\php66B.tmp
                )
        )

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

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

            [3] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )
        )

    [size] => Array
        (
            [1] => Array
                (
                    [0] => 133659
                )

            [2] => Array
                (
                    [0] => 148940
                    [1] => 211420
                )

            [3] => Array
                (
                    [0] => 3939511
                    [1] => 4742470
                    [2] => 4345899
                )
        )

)

我希望它看起来像这样

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => 500aqua.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5F9.tmp
                    [error] => 0
                    [size] => 133659

                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [name] => 500beige.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5FA.tmp
                    [error] => 0
                    [size] => 148940
                )
            [1] => Array
                (
                    [name] => 500beige-zoom.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5FB.tmp
                    [error] => 0
                    [size] => 211420
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [name] => P1040485.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php60C.tmp
                    [error] => 0
                    [size] => 148940
                )
            [1] => Array
                (
                    [name] => P1040486.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php63B.tmp
                    [error] => 0
                    [size] => 148940
                )
            [2] => Array
                (
                    [name] => P1040487.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php66B.tmp
                    [error] => 0
                    [size] => 148940
                )
        )
)

我已经尝试了以下代码,但它似乎只能获得数组中的最后3个。

foreach ($data as $d) {
    foreach ($d as $key => $e) {
        foreach ($e as $key2 => $f) {
            $result[$key2][$key] = $f;
        }
    }
}

Array
(
    [0] => Array
        (
            [name] => P1040485.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php60C.tmp
            [error] => 0
            [size] => 3939511
        )

    [1] => Array
        (
            [name] => P1040486.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php63B.tmp
            [error] => 0
            [size] => 4742470
        )

    [2] => Array
        (
            [name] => P1040487.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php66B.tmp
            [error] => 0
            [size] => 4345899
        )

)

提前致谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

foreach ($data as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        foreach ($value2 as $key3 => $value3) {
            $result[$key2][$key3][$key1] = $value3;
        }
    }
}

您忘记在第一级包含密钥($ key1):名称,类型等。

答案 1 :(得分:1)

试试这个:

foreach ($data as $field => $field_values) {
    foreach (array_values($field_values) as $index_1 => $index_1_values) {
        foreach ($index_1_values as $index_2 => $value) {
            $result[$index_1][$index_2][$field] = $value;
        }
    }
}