如何在PHP中更改数组格式

时间:2013-11-25 11:01:10

标签: php arrays format resultset

我想从

更改我的数组格式
Array
(
    [profileImg] => Array
        (
            [name] => Array
                (
                    [0] => index.jpg
                    [1] => index1.jpg
                )

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

            [tmp_name] => Array
                (
                    [0] => D:\xampp\tmp\php714F.tmp
                    [1] => D:\xampp\tmp\php714F.tmp
                )

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

            [size] => Array
                (
                    [0] => 13543
                    [1] => 23543
                )

        )

)

array(
    [name] => index.jpg,
    [type] => image/jpeg,
    [tmp_name] => D:\xampp\tmp\php714F.tmp,
    [error] => 0,
    [size] => 13543
)

array(
    [name] => index1.jpg,
    [type] => image/jpeg,
    [tmp_name] => D:\xampp\tmp\php714F.tmp,
    [error] => 0,
    [size] => 23543
)

我从我的表格中得到这个

<input name="profileImg[]" type="file" />

任何帮助都会很感激,谢谢

3 个答案:

答案 0 :(得分:4)

这就是你想要的。

<?php

$result = array();
foreach ($list['profileImg'] as $key => $keyList)
{
    foreach ($keyList as $index => $value)
    {
        $result[$index][$key] = $value;
    }
}
print_r($result);

答案 1 :(得分:1)

这应该有用。

$result = [];
foreach($_FILES['profileImg'] as $k => $d){
 foreach($d as $key => $val){
   $result[$key][$k] = $d[$key];
  }
}

print_r($result);返回,

Array
(
    [0] => Array
        (
            [name] => index.jpg
            [type] => image/jpeg
            [tmp_name] => D:\xampp\tmp\php714F.tmp
            [error] => 0
            [size] => 13543
        )

    [1] => Array
        (
            [name] => index1.jpg
            [type] => image/jpeg
            [tmp_name] => D:\xampp\tmp\php714F.tmp
            [error] => 0
            [size] => 23543
        )

)

答案 2 :(得分:0)

$result = array();
foreach ($_FILES['profileImg'] as $name => $data) {
    foreach ($data as $key => $item) {
        $result[$key][$name] = $item;
    }
}

应该这样做。