首页 > 编程 > PHP > 正文

二维数组排序:array_orderby(php官网评论)

2019-11-08 20:05:11
字体:
来源:转载
供稿:网友
/**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(). */<?phpfunction 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.<?php$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);?>

转自:http://php.net/manual/zh/function.array-multisort.php 第一条评论就是。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表