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 !!