单独的嵌套数组

时间:2011-03-29 21:38:54

标签: php arrays

我想根据文件的数量将这个数组分成多个数组。

Array
(
    [files] => Array
        (
            [name] => Array
                (
                    [0] => pic1.png
                    [1] => pic2.png
                )

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

            [tmp_name] => Array
                (
                    [0] => C:\Windows\Temp\php893F.tmp
                    [1] => C:\Windows\Temp\php895F.tmp
                )

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

            [size] => Array
                (
                    [0] => 777180
                    [1] => 734111
                )

        )

)

我希望它看起来像这样:

Array
(
    [files] => Array
        (
            [0] => array
                (
                    [name] => pic1.png
                    [type] => image/png
                    [tmp_name] => C:\Windows\Temp\php893F.tmp
                    [error] => 0
                    [size] => 777180
                )

            [1] => array
                (
                    [name] => pic2.png
                    [type] => image/png
                    [tmp_name] => C:\Windows\Temp\php895F.tmp
                    [error] => 0
                    [size] => 734111
                )
        )
)

1 个答案:

答案 0 :(得分:3)

如果您的实际输入数组具有与您在问题中提供的结构和键相同的结构和键,那么这段代码应该可以工作:

$separated = array();

foreach ($array['files'] as $property => $values)
{
  foreach ($values as $key => $value)
  {
    $separated[$key][$property] = $value;
  }
}