二维数组按照某个键值排序
2011-09-27 17:50
363 查看
//二维数组,按其中某个键值排序 /* * @param array $array the array to sort. * @param string $key_name the first item to sort by. * @param string $sort_order the order to sort by("SORT_ASC"|"SORT_DESC") * @param string $sort_type the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING") */ function multisort(&$array, $key_name, $sort_order = 'SORT_ASC', $sort_type = 'SORT_REGULAR') { if (!is_array($array)) { return $array; } // Get args number. $arg_count = func_num_args(); // Get keys to sort by and put them to SortRule array. for ($i = 1; $i < $arg_count; $i++) { $arg = func_get_arg($i); if (!eregi('SORT', $arg)) { $key_name_list[] = $arg; $sort_rule[] = '$'.$arg; } else { $sort_rule[] = $arg; } } // Get the values according to the keys and put them to array. foreach ($array as $key => $info) { foreach ($key_name_list as $key_name) { ${$key_name}[$key] = $info[$key_name]; } } // Create the eval string and eval it. $eval_str = 'array_multisort('.implode(',', $sort_rule).', $array);'; eval($eval_str); return $array; }
例子
$data = Array ( 0 => Array ( 'title' => '<font color="red">U8</font>-HR宁夏', 'linkline' => 'space.php?uid=4018&do=album&id=498', 'time' => 1275012236, 'content' => '<font color="red">U8</font>-HR宁夏', 'from' => 'album' ), 1 => Array ( 'title' => '<font color="red">u8</font> AII-in-One全国巡展-济宁站', 'linkline' => 'space.php?uid=2770&do=album&id=339', 'time' => 1268628887, 'content' => '<font color="red">u8</font> AII-in-One全国巡展-济宁站', 'from' => 'album' ), 2 => Array ( 'title' => '<font color="red">U8</font> ALL-IN-ONE巡展-河南站', 'linkline' => 'space.php?uid=1282&do=album&id=359', 'time' => 1269333609, 'content' => '<font color="red">U8</font> ALL-IN-ONE巡展-河南站', 'from' => 'album' ), 3 => Array ( 'title' => '先睹为快 <font color="red">U8</font> All-in-One体验光盘', 'linkline' => 'space.php?uid=3857&do=album&id=665', 'time' => 1285049939, 'content' => '先睹为快 <font color="red">U8</font> All-in-One体验光盘', 'from' => 'album' ) ) multisort($data, 'time', 'SORT_DESC');
相关文章推荐
- PHP 对一个给定的二维数组按照指定的键值进行排序
- php二维数组按照键值排序的方法
- 【php】对给定二维数组按照指定的键值进行排序
- PHP按照某个键值对二维数组进行排序
- PHP 对一个给定的二维数组按照指定的键值进行排序
- php二维数组按照某个键值排序
- 二维数组按照其内层数组的某个键值排序
- php二维数组按照键值排序的方法
- PHP 的二维数组,按照某个键的值进行排序
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请按成 一个函数,输入这样一个二维数组和一个整数,判断数组中是否含有该整数
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- numpy中二维数组按照某列、某行排序
- php 对二维数组的指定键值进行排序
- 二维数组按指定的键值排序
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都按照从左到右的递增的顺序排序,输入这样的一个数组和一个数,判断数组中是否包含这个数
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 如何给二维数组按照具体某行,某列排序
- (PHP实现剑指offer)在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 根据数组中的某个键值大小进行排序,仅支持二维数组
- PHP按指定键值对二维数组进行排序的方法