2

大家知道,在新的 HTML5 中,您可以非常轻松地上传多个文件。但我的问题是如何按列“名称”对 $_FILES 数组进行排序?这是 $_FILES 的示例数组

 Array (
[item_file] => Array (
    [name] => Array (
        [0] => redphotonzillion1.jpg
        [1] => redphotonzillion2.jpg
        [2] => redphotonzillion3.jpg
        [3] => redphotonzillion4.jpg
        [4] => redphotonzillion5.jpg
        [5] => redphotonzillion6.jpg
    )
    [type] => Array (
        [0] => image/jpeg
        [1] => image/jpeg
        [2] => image/jpeg
        [3] => image/jpeg
        [4] => image/jpeg
        [5] => image/jpeg
    )
    [tmp_name] => Array (
        [0] => /var/www/clients/client0/web6/tmp/phpi4i5fE
        [1] => /var/www/clients/client0/web6/tmp/phpzHwagk
        [2] => /var/www/clients/client0/web6/tmp/phpYpAgg0
        [3] => /var/www/clients/client0/web6/tmp/phpAIdngG
        [4] => /var/www/clients/client0/web6/tmp/phpyamugm
        [5] => /var/www/clients/client0/web6/tmp/phpFvWBg2
    )
    [error] => Array (
        [0] => 0
        [1] => 0
        [2] => 0
        [3] => 0
        [4] => 0
        [5] => 0
    )
    [size] => Array (
        [0] => 7990
        [1] => 8985
        [2] => 8014
        [3] => 7621
        [4] => 7023
        [5] => 7151
    )
)

)

我想要的只是按名称对这个数组进行排序

4

1 回答 1

2

对于这种特定情况,PHP 提供了正确的功能array_multisort()

它可以通过首先对第一个数组进行排序,然后将结果顺序也应用于所有其他数组来对多维数组(或多个平面数组)进行排序(请参阅文档以获取示例)。

array_multisort(
  // Array used to sort + optional parameters
  $_FILES['item_file']['name'], SORT_ASC, SORT_STRING

  // All other arrays to be sorted in the same order
  $_FILES['item_file']['type'],
  $_FILES['item_file']['tmp_name'],
  $_FILES['item_file']['error'],
  $_FILES['item_file']['size']
);

这种方法将保留所有元信息(type, tmp_name, error, size)分配给文件的相应索引,name以便可以使用它。

于 2013-10-24T20:34:57.860 回答