获取所需格式的PHP数组

时间:2014-08-07 11:26:44

标签: php arrays

我从array获取api request图片的格式如下所示。这个array非常难以遍历并且难以上传。我想在单个索引中包含array,其中包含单个图像的所有信息。我有以下错误:

Array
(
    [name] => Array
        (
            [0] => prog-programming_00267956.png
            [1] => programming-wallpaper_17695.jpg
        )

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

    [tmp_name] => Array
        (
            [0] => /tmp/php0azGbU
            [1] => /tmp/phpyByFGB
        )

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

    [size] => Array
        (
            [0] => 587659
            [1] => 83817
        )

)

我必须将其转换为:

    Array
    (
       [0] => Array
      (
        [name] => prog-programming_00267956.png
        [type] => image/png
        [tmp_name] => /tmp/phphSShmd
        [error] => 0
        [size] => 587659
      )
   [1] => Array
      (
        [name] => prog-programming_00267956.png
        [type] => image/png
        [tmp_name] => /tmp/phphSShmd
        [error] => 0
        [size] => 587659
      )
    )

那么如何将数组转换为上述格式。

1 个答案:

答案 0 :(得分:7)

非常简单。

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