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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么