PHP多维数组排序array详解
发布:smiling 来源: PHP粉丝网 添加日期:2021-08-20 11:31:46 浏览: 评论:0
这篇文章主要为大家详细介绍了PHP多维数组排序array的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
PHP数组Array按字段排序
- /**
- * Sort array by filed and type, common utility method.
- * @param array $data
- * @param string $sort_filed
- * @param string $sort_type SORT_ASC or SORT_DESC
- */
- public function sortByOneField($data, $filed, $type)
- {
- if (count($data) <= 0) {
- return $data;
- }
- foreach ($data as $key => $value) {
- $temp[$key] = $value[$filed];
- }
- array_multisort($temp, $type, $data);
- return $data;
- }
PHP数组Array按二维排序,先按第一个字段排序,再按第二个字段排序
- /**
- * Sort array by filed and type, common utility method.
- * @param array $array
- * @param string $filed1
- * @param string $type1 SORT_ASC or SORT_DESC
- * @param string $filed2
- * @param string $type2 SORT_ASC or SORT_DESC
- */
- public function sortByTwoFiled($data, $filed1, $type1, $filed2, $type2)
- {
- if (count($data) <= 0) {
- return $data;
- }
- foreach ($data as $key => $value) {
- $temp_array1[$key] = $value[$filed1];
- $temp_array2[$key] = $value[$filed2];
- }
- array_multisort($temp_array1, $type1, $temp_array2, $type2, $data);
- return $users;
- }
sortMultiArray()最多支持3维数组排序,当然可以扩充的,自定义方法重载实现对多维数字的排序,这里的多维是说数据的多个字段。
使用方法:
1. sortMultiArray($data, [‘score' => SORT_DESC])
2. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC])
3. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC, ‘name' => SORT_ASC])
- /**
- * Sort multi array by filed and type.
- * @param data $array
- * @param condition $array
- */
- public function sortMultiArray(&$data, $condition)
- {
- if (count($data) <= 0 || emptyempty($condition)) {
- return $data;
- }
- $dimension = count($condition);
- $fileds = array_keys($condition);
- $types = array_values($condition);
- switch ($dimension) {
- case 1:
- $data = $this->sort1Dimension($data, $fileds[0], $types[0]);
- break;
- case 2:
- $data = $this->sort2Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1]);
- break;
- default:
- $data = $this->sort3Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1], $fileds[2], $types[2]);
- break;
- }
- return $data;
- }
- public function sort1Dimension(&$data, $filed, $type)
- {
- if (count($data) <= 0) {
- return $data;
- }
- foreach ($data as $key => $value) {
- $temp[$key] = $value[$filed];
- }
- array_multisort($temp, $type, $data);
- return $data;
- }
- public function sort2Dimension(&$data, $filed1, $type1, $filed2, $type2)
- {
- if (count($data) <= 0) {
- return $data;
- }
- foreach ($data as $key => $value) {
- $sort_filed1[$key] = $value[$filed1];
- $sort_filed2[$key] = $value[$filed2];
- }
- array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $data);
- return $data;
- }
- public function sort3Dimension(&$data, $filed1, $type1, $filed2, $type2, $filed3, $type3)
- {
- if (count($data) <= 0) {
- return $data;
- }
- foreach ($data as $key => $value) {
- $sort_filed1[$key] = $value[$filed1];
- $sort_filed2[$key] = $value[$filed2];
- $sort_filed3[$key] = $value[$filed3];
- }
- array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $sort_filed3, $type3, $data);
- return $data;
- }
Tags: PHP多维数组排序 array
相关文章
- ·php多维数组排序与操作函数(2014-08-05)
- ·php一维数组排序与多维数组排序(2014-09-09)
- ·PHP多维数组排序实现程序代码(2014-09-14)
- ·array_multisort 多维数组排序函数(2014-09-18)
- ·PHP实现指定字段的多维数组排序函数分享(2021-05-15)
- ·php中array_multisort对多维数组排序的方法(2021-06-11)
- ·php in_array 语法(2013-11-12)
- ·解决php array数组生成xml文件汉字编码问题(2013-12-07)
- ·php array_flip()与array_uniqure()删除数组重复元素(2014-01-08)
- ·in_array函数的用法(2014-01-14)
- ·php 数组值求和之array_sum()函数(2014-01-15)
- ·php数组操作函数之array_push()函数用法与定义(2014-01-15)
- ·php 删除数组最后一个元素 array_pop()函数用法(2014-01-15)
- ·php中Array数组函数用法详解(2014-02-16)
- ·PHP 数组合并相加与array_merge 的区别(2014-02-22)
- ·php数组操作学习笔记(2014-03-07)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)