Home > Backend Development > PHP Tutorial > php中多维数组按指定value排序的实现代码_php技巧

php中多维数组按指定value排序的实现代码_php技巧

WBOY
Release: 2016-05-16 20:38:09
Original
1084 people have browsed it

呵呵,业务需要按多维数组中某个元素进行排序,在PHP中也是非常容易实现的,一个函数调用一个回调函数就搞定了。贴出代码:

复制代码 代码如下:

$arr = array(   

    'index'=>array( 'name'=>'首页','order'=>3),

    'intro'=>array( 'name'=>'企业概况','order'=>2),

    'news'=>array( 'name'=>'新闻动态','order'=>1 ),

    'product'=>array( 'name'=>'产品中心','order'=>4 ),

    'message'=>array( 'name'=>'访客留言','order'=>7 ),

    'position'=>array( 'name'=>'人才招聘','order'=>6),

    'contact'=>array( 'name'=>'联系我们','order'=> 5 )

);

uasort($arr, 'cmp');

public function cmp($a, $b){

   return $a['order'] - $b['order'];

}


这时$arr就是以order大小排序了,呵呵……
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template