Home  >  Article  >  Backend Development  >  How to implement multi-dimensional array sorting in php

How to implement multi-dimensional array sorting in php

墨辰丷
墨辰丷Original
2018-05-24 16:22:511560browse

This article mainly introduces the method of implementing multi-dimensional array sorting in PHP, and analyzes PHP's related skills for sorting multi-dimensional arrays in the form of examples. Friends in need can refer to the following

The examples in this article describe the implementation of PHP Methods for sorting multidimensional arrays. Share it with everyone for your reference, the details are as follows:

//定义一个学生数组
$students = array(
  256=>array('name'=>'jon','grade'=>98.5),
  2=>array('name'=>'vance','grade'=>85.1),
  9=>array('name'=>'stephen','grade'=>94.0),
  364=>array('name'=>'steve','grade'=>85.1),
  68=>array('name'=>'rob','grade'=>74.6),
);
//按照名称进行排序
function name_sort($x, $y)
{
  return strcasecmp($x['name'],$y['name']);
}
//按照成绩进行排序
function grade_sort($x,$y)
{
  return ($x['grade'] > $y['grade']);
}
//应用
uasort($students, name_sort);
uasort($students, grade_sort);

The above is the entire content of this article, I hope it will be helpful to everyone’s study .


Related recommendations:

Front-end SortDetailed explanation of algorithm examples

PHP method to implement multi-dimensional array sorting according to a certain key value

PHP two-dimensional associative array sorting according to one of the fields Methods

The above is the detailed content of How to implement multi-dimensional array sorting in php. For more information, please follow other related articles on the PHP Chinese website!

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