Blogger Information
Blog 55
fans 0
comment 0
visits 50480
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP-usort()多维数组的排序-0827
Bean_sproul
Original
764 people have browsed it

usort() 使用用户自定义的比较函数对数组进行排序。

usort(array,myfunction);

实例

<?php
// usort()多维数组的排序
$stu = [
  ['name'=>'林心如','grade'=>98],
  ['name'=>'范冰冰','grade'=>55],
  ['name'=>'左小青','grade'=>73],
];
echo '<pre>';
echo '排序之前:',var_export($stu,true),'<hr>';
//用户自定义排序规则
usort($stu, function ($m, $n) {
    //$m ,$n实际上还是一个数组
   return strcmp($m['grade'], $n['grade']);
});
echo '排序之后:',var_export($stu,true),'<hr>';

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post