php怎么自动加数字排序,如何使用PHP按数字顺序对该数组排序

我正在尝试按文件名对文件列表进行排序.

这是我的数组:

Array

(

[5] =>

[4] => Array

(

[id] => 194

[filename] => 1.2 Organogram company BV.pptx

[name] => undefined

[path] => /home/website/public_html/fileupload/company/organisatie/

[cat_id] => 297

[error] => 0

)

[1] => Array

(

[id] => 195

[filename] => 1.2 VOL VCA R. company 13-12-2024.docx

[name] => undefined

[path] => /home/website/public_html/fileupload/company/organisatie/

[cat_id] => 297

[error] => 0

)

[0] => Array

(

[id] => 196

[filename] => 1.2 MVK- diploma 2016 Piet Schipaanboord.jpg

[name] => undefined

[path] => /home/website/public_html/fileupload/company/organisatie/

[cat_id] => 297

[error] => 0

)

[3] => Array

(

[id] => 200

[filename] => 1.1 Beleidsverklaring 20-09-2018.docx

[name] => undefined

[path] => /home/website/public_html/fileupload/company/organisatie/

[cat_id] => 297

[error] => 0

)

[2] => Array

(

[id] => 201

[filename] => 1.2 Functieomschrijving VGM-functionaris.docx

[name] => undefined

[path] => /home/website/public_html/fileupload/company/organisatie/

[cat_id] => 297

[error] => 0

)

)

如您所见,它现在的排序如下:

1.2

1.2

1.2

1.1

1.2

我该如何正确排序呢?像这样:

1.1

1.2

1.2

1.2

1.2

我已经尝试过这样的方式:

$getfiles = "SELECT * FROM files1 WHERE cat_id = 20";

$getfilescon = $conn->query($getfiles);

while($getfiles[] = $getfilescon->fetch_assoc());

asort($getfiles, $getfiles['filename']);

或像这样:

asort($getfiles['filename']);

或这个:

asort($getfiles[]['filename']);

但是,没有任何东西能提供理想的结果.

解决方法:

您需要使用usort并创建自己的排序优先级.

它看起来应该像这样.

function customSort($a, $b) {

if ($a['filename'] == $b['filename']) {

return 0;

}

return ($a['filename'] < $b['filename']) ? -1 : 1;

}

usort($array, "customSort");

或者,如果您使用PHP 7

usort($array, function (array $a, array $b) {

return $a['filename'] <=> $b['filename'];

});

标签:arrays,php,sorting

来源: https://codeday.me/bug/20191025/1924841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值