php二维数组中如何对其中一维数组中的某个字段进行排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php二维数组中如何对其中一维数组中的某个字段进行排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5422字,纯文字阅读大概需要8分钟。
内容图文
![php二维数组中如何对其中一维数组中的某个字段进行排序](/upload/InfoBanner/zyjiaocheng/138/993a3740464f4e59b59d05db03864a51.jpg)
本篇文章给大家分享的内容是php二维数组中如何对其中一维数组中的某个字段进行排序,有着一定的参考价值,有需要的朋友可以参考一下
这里获取到的数据库里的数据
public function hot_sort(){ $type = input('type'); $list = $this->get_hot_sort($type); $res = $list->data; $re = $this->object_array($res); foreach ($re as $k=>$v){ $data= Db::name('merchants')->field('share_num')->where(['gl_merchants_id'=>$v['user_id']])->find(); $re[$k]['share_num'] = $data['share_num']; } $result = $this->quick_sort($re); //这里调用方法根据某个字段进行排序 success($result); }
========================================================================
方法进行排序 /** * @param $arrUsers * @return mixed *二维数组某个字段进行排序 */ function quick_sort($arrUsers) { $sort = array( 'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序 'field' => 'share_num', //排序字段 ); $arrSort = array(); foreach($arrUsers AS $k => $v){ foreach($v AS $key=>$value){ $arrSort[$key][$k] = $value; } } if($sort['direction']){ array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrUsers); } return $arrUsers; } 原来的排序结果 { "status": "ok", "data": [ { "user_id": "29", "shop_id": 7, "total_money": 40000, "user_name": "b1", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "16", "shop_id": 6, "total_money": 15000.01, "user_name": "b7", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg", "share_num": "" }, { "user_id": "13", "shop_id": 4, "total_money": 5000, "user_name": "gelu1234", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "56", "shop_id": 20, "total_money": 70, "user_name": "HECAI", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "32", "shop_id": 9, "total_money": 37.1, "user_name": "baxianqiao", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "670000" }, { "user_id": "8", "shop_id": 1, "total_money": 0, "user_name": "SXMY", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "35", "shop_id": 8, "total_money": 0, "user_name": "self01", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg", "share_num": "" } ] } 现在排序的结果 { "status": "ok", "data": [ { "user_id": "32", "shop_id": 9, "total_money": 37.1, "user_name": "baxianqiao", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "670000" }, { "user_id": "8", "shop_id": 1, "total_money": 0, "user_name": "SXMY", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "13", "shop_id": 4, "total_money": 5000, "user_name": "gelu1234", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "16", "shop_id": 6, "total_money": 15000.01, "user_name": "b7", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg", "share_num": "" }, { "user_id": "29", "shop_id": 7, "total_money": 40000, "user_name": "b1", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" }, { "user_id": "35", "shop_id": 8, "total_money": 0, "user_name": "self01", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20180122/1516586013148395.jpg!120x120.jpg", "share_num": "" }, { "user_id": "56", "shop_id": 20, "total_money": 70, "user_name": "HECAI", "user_headimg": "https://ucenter.ttzxh.com/image.php/ucenter/data/upload/media/plantform/image/20171213/1513144055861391.jpg!120x120.jpg", "share_num": "" } ] }
以上就是php二维数组中如何对其中一维数组中的某个字段进行排序的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的php二维数组中如何对其中一维数组中的某个字段进行排序全部内容,希望文章能够帮你解决php二维数组中如何对其中一维数组中的某个字段进行排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。