Home >Backend Development >PHP Tutorial >A small example of php sorting a two-dimensional array according to key value

A small example of php sorting a two-dimensional array according to key value

WBOY
WBOYOriginal
2016-07-25 08:55:451026browse
  1. /* Two-dimensional array is sorted by the specified key value
  2. * $array array
  3. * $key sort key value
  4. * $type sorting method
  5. * @edit: bbs.it-home. org
  6. */
  7. function array_sort($arr, $keys, $type = 'desc') {
  8. $keysvalue = $new_array = array();
  9. foreach ($arr as $k => $v) {
  10. $ keysvalue[$k] = $v[$keys];
  11. }
  12. if ($type == 'asc') {
  13. asort($keysvalue);
  14. } else {
  15. arsort($keysvalue);
  16. }
  17. reset( $keysvalue);
  18. foreach ($keysvalue as $k => $v) {
  19. $new_array[$k] = $arr[$k];
  20. }
  21. return $new_array;
  22. }
Copy code

>>> For more information, please view the complete list of php array sorting methods



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn