首頁 >php教程 >php手册 >二维数组按照其中一个键值排序

二维数组按照其中一个键值排序

WBOY
WBOY原創
2016-06-06 20:11:25946瀏覽

二维数组按照其中一个键值排序 : ?php function multisort( }// 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 (!preg_match(

二维数组按照其中一个键值排序

<?php 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 (!preg_match('/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' => '
 U8
 -HR宁夏',
 'linkline' => 'space.php?uid=4018&do=album&id=498',
 'time' => 1275012236,
 'content' => '
 U8
 -HR宁夏',
 'from' => 'album'
 ),
 1 => Array
 (
 'title' => '
 u8
  AII-in-One全国巡展-济宁站',
 'linkline' => 'space.php?uid=2770&do=album&id=339',
 'time' => 1268628887,
 'content' => '
 u8
  AII-in-One全国巡展-济宁站',
 'from' => 'album'
 ),
 2 => Array
 (
 'title' => '
 U8
  ALL-IN-ONE巡展-河南站',
 'linkline' => 'space.php?uid=1282&do=album&id=359',
 'time' => 1269333609,
 'content' => '
 U8
  ALL-IN-ONE巡展-河南站',
 'from' => 'album'
 ),
 3 => Array
 (
 'title' => '先睹为快
 U8
  All-in-One体验光盘',
 'linkline' => 'space.php?uid=3857&do=album&id=665',
 'time' => 1285049939,
 'content' => '先睹为快
 U8
  All-in-One体验光盘',
 'from' => 'album'
 )
 );
 print_r(multisort($data, 'time', 'SORT_DESC'));
?>
 

这样就将$data数组按照自己的time键重新排序了。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn