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 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错