Home  >  Article  >  Backend Development  >  A simple sorting of multidimensional arrays in PHP_PHP tutorial

A simple sorting of multidimensional arrays in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:25810browse

To sort by an element in a multi-dimensional array, it is very easy to implement in PHP. A function can call a callback function. You can also use PHP's own function

such as array_multisort($a,$ b), $a, $b are two arrays. If after sorting, the third element of $a array is ranked first, then the third element of $b will be ranked first regardless of its size in $b. In the first place. Take a look at the results of the program below:

The code is as follows
 代码如下 复制代码

$a =array(100,80,50,10,0);
$b = array("c","f","q","e","z");
array_multisort($a,$b);
var_dump($a);
var_dump($b);
?>

Copy code

 代码如下 复制代码

array(5) { [0]=> int(0) [1]=> int(10) [2]=> int(50) [3]=> int(80) [4]=> int(100) }
array(5) { [0]=> string(1) “z” [1]=> string(1) “e” [2]=> string(1) “q” [3]=> string(1) “f” [4]=> string(1) “c” }

$a =array(100,80,50,10,0);
$b = array("c","f","q","e","z");
array_multisort($a,$b);
var_dump($a);
var_dump($b);
?>

 代码如下 复制代码

$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'];
 
}

Run result:
The code is as follows
Copy code

array(5) { [0]=> int(0) [1]=> int(10) [2]=> int(50) [3]=> int(80) [4 ]=> int(100) }
array(5) { [0]=> string(1) “z” [1]=> string(1) “e” [2]=> string(1) “q” [3]=> string(1) “f” [4]=> string(1) “c” }

Custom function to implement sorting
The code is as follows Copy code
$arr = array( 

'index'=>array( 'name'=>'Homepage','order'=>3),

'intro'=>array( 'name'=>'Company Profile','order'=>2),

'news'=>array( 'name'=>'News','order'=>1 ),

'product'=>array( 'name'=>'Product Center','order'=>4 ),

'message'=>array( 'name'=>'Guest message','order'=>7 ),

'position'=>array( 'name'=>'Talent Recruitment','order'=>6),

'contact'=>array( 'name'=>'Contact us','order'=> 5 )

);

uasort($arr, 'cmp');

public function cmp($a, $b){

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

}
At this time $arr is sorted by order size, haha...
http://www.bkjia.com/PHPjc/633103.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633103.htmlTechArticleTo sort by an element in a multi-dimensional array, it is also very easy to implement in PHP. One function calls a The callback function is enough, or you can use PHP’s built-in functions such as array_multisor...
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