PHP_IloveU (1) php food and clothing parents: Use the usort () function to sort two-dimensional arrays

Source: Internet
Author: User
PHP_IloveU (1) php food and clothing parents: Use the usort () function to sort two-dimensional arrays // PHP_ I love U (1) php clothes and food parent: Use the usort () function to sort two-dimensional arrays
// Use the usort () function in PHP to sort two-dimensional arrays:
// [] [] = A [] [1] = length; a [] [2] = views clicks

$ AAlong = 10; // variable $ AAlong defines the array length
// For ($ I = 0; $ I <$ AAlong; ++ $ I) // used for multi-dimensional array extension...
//{
For ($ j = 0; $ j <$ AAlong; ++ $ j) // for j22 // assign a value to the array (with a random number)
{
$ AA [$ j] [0] = $ j;
$ AA [$ j] [1] = rand (10, 40 );
$ AA [$ j] [2] = rand (0,112 );
} // For j22
///} // For11


// First display the array (original) value
For ($ j = 0; $ j <$ AAlong; ++ $ j) // for j22
{
Echo "no.". $ AA [$ j] [0]. ", length". $ AA [$ j] [1];
Echo ", its clicks". $ AA [$ j] [2];
Echo "\ r \ n ";
} // For j22


// Sort by usort (array variable, sorting function!
Usort ($ AA, function ($ a, $ B ){
$ Al = ($ a [2]); // if no parentheses are added, of course. parentheses indicate that expressions or functions can be used here...
$ Bl = $ B [2];
If ($ al = $ bl)
Return 0;
Return ($ al> $ bl )? -1: 1; // This is the forward order (sorting). What about the reverse order? Write: // return ($ al <$ bl )? -1: 1; // in reverse order !!
});


Echo "\ r \ n and then display the sorted: \ n ";
For ($ j = 0; $ j <$ AAlong; ++ $ j) // for j23
{
Echo "no.". $ j. ": no.". $ AA [$ j] [0]. ", length". $ AA [$ j] [1];
Echo ", its clicks". $ AA [$ j] [2];
Echo "\ r \ n ";
} // For j23


Echo "OK success. I like php & usort ()! ";


// The usort () function is really powerful. // thanks to the php language, it ensures countless coders like me... let our family have a meal !! :)

// Some friends say you forgot to write?>" To end the php closed language, I am writing from the habit of Drupal "?> "An error occurs. Since this parameter is not set,"?> "No error, then we will... Write "?>" again when necessary" Right !!

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.