dongzou9047 2016-12-19 05:46
浏览 50
已采纳

按大小值排序多维数组

When uploading images via HTTP I get the following array. How can I sort them by the size of the images in descending order, so the biggest size images would be uploaded the first and the smallest size the last?

    Array
(
    "name" => Array
        (
            [0] => 1.jpg
            [1] => 2.jpg
            [2] => 3.jpg
        )

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

    ["tmp_name"] => Array
        (
            [0] => e7d31fc0
            [1] => qsdf0sdf
            [2] => s0sdfsfs
        )

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

    ["size"] => Array
        (
            [0] => 20000
            [1] => 30000
            [2] => 40000
        )
)

As a result, the output should be as follows:

    Array
(
    "name" => Array
        (
            [0] => 3.jpg
            [1] => 2.jpg
            [2] => 1.jpg
        )

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

    ["tmp_name"] => Array
        (
            [0] => s0sdfsfs
            [1] => qsdf0sdf
            [2] => e7d31fc0
        )

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

    ["size"] => Array
        (
            [0] => 40000
            [1] => 30000
            [2] => 20000
        )
)
  • 写回答

2条回答 默认 最新

  • dongzenglin8292 2016-12-19 06:33
    关注

    I suppose, you will process those files in a foreach loop.

    How about the code below?

    //get files in array
    $files = $_FILES['formFieldName'];
    $sizes = $files['size'];
    arsort($sizes); //sort in descending order but will preserve the keys
    foreach ($sizes as $key => $size) {
        $fileName = $files['name'][$key];
        $fileSize = $size;
        $fileType = $files['type'][$key];
        $fileTmpName = $files['tmp_name'][$key];
        $fileError = $files['error'][$key];
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决