为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Laravel 5.7 中对数组进行排序

如何在 Laravel 5.7 中对数组进行排序

PHP
慕标5832272 2022-01-02 16:27:45
我有一个由外部 API 产生的数组。我想使用 ASC 格式对此数组进行排序,但是在我的函数上对其进行测试时,排序结果与我的数组数据相同。它没有排序。我正在使用 Laravel 5.7 来做到这一点。这是我的功能:public function sortData(){    $arrayData = [        {"id":"950328","type":"CMATYPE ","num":"10"},        {"id":"950328","type":"CM007","num":"20"},        {"id":"950328","type":"CM021B ","num":"30"},        {"id":"950328","type":"CM047","num":"40"},        {"id":"950328","type":"CM-MMFGF","num":"50"},        {"id":"950328","type":"CM152","num":"60"},        {"id":"950328","type":"CM179 ","num":"70"},        {"id":"950328","type":"CM029A","num":"80"},        {"id":"950328","type":"CM033C ","num":"90"},        {"id":"950328","type":"CM033E","num":"100"},        {"id":"950328","type":"CM001 ","num":"110"},        {"id":"950328","type":"CM012","num":"120"},        {"id":"950328","type":"CM202 ","num":"130"},        {"id":"950328","type":"CM203","num":"140"},          {"id":"950328","type":"CM205 ","num":"150"},        {"id":"950328","type":"CASE","num":"160"}    ];    foreach ($arrayData as $key => $value){        $sorted = collect($value)->sortBy($value['type']);        echo "Data From Server : ".$sorted['type']."\n";    }}这是结果:Data From Server : CM007Data From Server : CM021BData From Server : CM047Data From Server : CM-MMFGFData From Server : CM152Data From Server : CM179Data From Server : CM029AData From Server : CM033CData From Server : CM033EData From Server : CM001Data From Server : CM012Data From Server : CM202Data From Server : CM203Data From Server : CM205Data From Server : CASE我的功能有问题吗?如何解决这个问题?
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

你可以在 Laravel 中使用sort by

$sorted = collect($arrayData)->sortBy('type');


查看完整回答
反对 回复 2022-01-02
  • 1 回答
  • 0 关注
  • 243 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信