A simple sort of multidimensional array in PHP _php tutorial

Source: Internet
Author: User
To sort by an element in a multidimensional array is also very easy to implement in PHP, a function call a callback function is done, it can also be PHP's own function

such as Array_multisort ($a, $b), $a, $b is two arrays, if after sorting, the 3rd element of the $a array is ranked first, then $b's third element regardless of his size in the $b will be ranked first. Look at the results of the program running below:

The code is as follows Copy Code

$a =array (100,80,50,10,0);
$b = Array ("C", "F", "Q", "E", "Z");
Array_multisort ($a, $b);
Var_dump ($a);
Var_dump ($b);
?>

Operation Result:

The code is as follows Copy Code

Array (5) {[0]=> int (0) [1]=> Int (ten) [2]=> int ([3]=> int) [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 Implementation Ordering

The code is as follows Copy Code

$arr = Array (

' Index ' =>array (' name ' = ' = ' home ', ' order ' =>3),

' Intro ' =>array (' name ' = ' Company profile ', ' Order ' =>2),

' News ' =>array (' name ' = ' Press ', ' Order ' =>1),

' Product ' =>array (' name ' = ' Products center ', ' Order ' =>4),

' Message ' =>array (' name ' = ' guest message ', ' Order ' =>7),

' Position ' =>array (' name ' = ' Jobs ', ' order ' =>6),

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

);

Uasort ($arr, ' CMP ');

Public Function cmp ($a, $b) {

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

}

At this time $arr is to order the size of order, hehe ...

http://www.bkjia.com/PHPjc/633103.html www.bkjia.com true http://www.bkjia.com/PHPjc/633103.html techarticle to sort by an element in a multidimensional array, in PHP is also very easy to implement, a function call a callback function is done, you can also PHP's own functions such as Array_multisor ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.