The example in this article describes a method that PHP implements to sort a key in a two-dimensional array. Share to everyone for your reference, specific as follows:
/** * Sorting Query result set * @access public * @param array $list Query results * @param string $field sorted field name * @param string $sortby sort Type (ASC forward sort desc Reverse sort Nat natural Sort) * @return Array */function list_sort_by ($list, $field, $sortby = ' asc ') {if is_
Array ($list)) {$refer = $resultSet = Array ();
foreach ($list as $i => $data) {$refer [$i] = & $data [$field];
Switch ($sortby) {case ' ASC '://forward sort asort ($refer);
Break
Case ' desc '://reverse order Arsort ($refer);
Break
Case ' NAT '://Natural sort natcasesort ($refer);
Break
foreach ($refer as $key => $val) {$resultSet [] = & $list [$key];
return $resultSet;
return false;
/** * Example * ask: in descending order of the ID key value of a two-dimensional array (i.e., the larger the ID)? */$list = Array (0 => array (' ID ' => 1, ' name ' => ' first '), 1 => array (' ID ' => 3, ' NA Me ' => ' third '), 2=> Array (' ID ' => 2, ' name ' => ' second '), 3 => array (' ID ' => 4, ' name ' => ' four '),);
Solution $new _list = list_sort_by ($list, ' id ', ' desc ');
Print_r ($new _list);
The results of the operation are as follows:
Array
(
[0] => array
(
[id] => 4
[name] => fourth
)
[1] => array
(
[id] = > 3
[name] => third
)
[2] => Array
(
[id] => 2
[name] => second
)
[3] =& Gt Array
(
[id] => 1
[name] => first
)
)
More interested in PHP related content readers can view the site topics: "PHP array" Operation Tips Encyclopedia, "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.