php自定义二维数组排序函数array_orderby用法示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-09-05 16:49:23 浏览: 评论:0
这篇文章主要介绍了php自定义二维数组排序函数array_orderby用法,结合实例形式分析了php针对二维数组进行排序的相关遍历、判定、排序等操作技巧,需要的朋友可以参考下。
本文实例讲述了php自定义二维数组排序函数array_orderby用法,分享给大家供大家参考,具体如下:
- <?php
- /**
- I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort().
- */
- function array_orderby()
- {
- $args = func_get_args();
- $data = array_shift($args);
- foreach ($args as $n => $field) {
- if (is_string($field)) {
- $tmp = array();
- foreach ($data as $key => $row)
- $tmp[$key] = $row[$field];
- $args[$n] = $tmp;
- }
- }
- $args[] = &$data;
- call_user_func_array('array_multisort', $args);
- return array_pop($args);
- }
- /*
- The sorted array is now in the return value of the function instead of being passed by reference.
- */
- $data[] = array('volume' => 67, 'edition' => 2);
- $data[] = array('volume' => 86, 'edition' => 1);
- $data[] = array('volume' => 85, 'edition' => 6);
- $data[] = array('volume' => 98, 'edition' => 2);
- $data[] = array('volume' => 86, 'edition' => 6);
- $data[] = array('volume' => 67, 'edition' => 7);
- // Pass the array, followed by the column names and sort flags
- $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
- print_r($sorted)
- ?>
运行结果:
- Array
- (
- [0] => Array
- (
- [volume] => 98
- [edition] => 2
- )
- [1] => Array
- (
- [volume] => 86
- [edition] => 1
- )
- [2] => Array
- (
- [volume] => 86
- [edition] => 6
- )
- [3] => Array
- (
- [volume] => 85
- [edition] => 6
- )
- [4] => Array
- (
- [volume] => 67
- [edition] => 2
- )
- [5] => Array
- (
- [volume] => 67
- [edition] => 7
- )
- )
Tags: php二维数组排序 array_orderby
相关文章
- ·php二维数组排序后获取最大值(2014-07-14)
- ·PHP二维数组排序问题详解(数组键值排序)(2014-07-15)
- ·php二维数组排序与遍历(2014-09-19)
- ·怎么用PHP实现二维数组按照键值排序(2020-03-30)
- ·php二维数组排序详解(2020-05-16)
- ·php对二维数组按指定键值key排序示例代码(2020-07-06)
- ·php对二维数组进行排序的简单实例(2020-08-11)
- ·一个PHP二维数组排序的函数分享(2020-08-27)
- ·PHP二维数组排序的3种方法和自定义函数分享(2020-11-08)
- ·php实例分享之二维数组排序(2020-12-25)
- ·php中二维数组排序问题方法详解(2021-06-16)
- ·php对二维数组进行相关操作(排序、转换、去空白等)(2021-06-25)
- ·PHP按指定键值对二维数组进行排序的方法(2021-06-30)
- ·PHP二维数组排序简单实现方法(2021-07-09)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)