1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?php
function arraySortByKey($array = array(), $key = " ", $asc = true){ $result = array(); foreach ($array as $k => $v) { $values[$k] = isset($v[$key]) ? $v[$key] : " "; } unset($v); $asc ? asort($values) : arsort($values);
foreach ($values as $k => $v){ $result[$k] = $array[$k]; } return $result; }
$data = array( array( 'post_id' => 1, 'title' => '如何学好PHP', 'reply_num' => 582 ), array( 'post_id' => 2, 'title' => 'PHP数组常用函数汇总', 'reply_num' => 182 ), array( 'post_id' => 3, 'title' => 'PHP字符串常用函数汇总', 'reply_num' => 982 ) );
$new_array = arraySortByKey($data, 'reply_num', false); echo '<pre>'; print_r($new_array); ?>
|