dqy92287 2019-03-10 12:00
浏览 470
已采纳

如何遍历$ _FILES数组的多维数组

I am using dropzone to get the files uploaded to my Folder. Successfully getting the array of files.

   foreach($_FILES as $file) {
       print_r($file);
    }

Current Output:

Array
(
    [name] => Array
        (
            [0] => Image.PNG
            [1] => sadssadsa.PNG
        )

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

    [tmp_name] => Array
        (
            [0] => C:\Users\CH MANAN\AppData\Local\Temp\php48B6.tmp
            [1] => C:\Users\CH MANAN\AppData\Local\Temp\php48B7.tmp
        )

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

    [size] => Array
        (
            [0] => 291647
            [1] => 112790
        )
)

Expected output:

array
(
    [0] => array
    (
        [name] => Image.PNG
        [type] => image/png
        [tmp_name] => C:\Users\CH MANAN\AppData\Local\Temp\php48B6.tmp
        [error] => 0
        [size] => 291647
    )

    [1] => array
    (
        [name] => sadssadsa.PNG
        [type] => image/png
        [tmp_name] => C:\Users\CH MANAN\AppData\Local\Temp\php48B7.tmp
        [error] => 0
        [size] => 112790
    )
)

Tried various loops in the parent loop but not getting the expected results. Someone can help here.

  • 写回答

4条回答 默认 最新

  • doujiao3998 2019-03-10 12:14
    关注

    You can use this:

    $keys = array_keys($_FILES); // get all the fields name
    $res = array_map(null, ...array_values($_FILES)); // group the array by each file
    $res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res); // insert the field name to result array
    

    Documentation:

    array-keys, array-map and array-combine

    Live example: 3v4l

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛