Php_i love u (1) php Food and clothing parents: Use the Usort () function to sort two-dimensional arrays

Source: Internet
Author: User
Tags php language

<?php
Php_i love u (1) php Food and clothing parents: Use the Usort () function to sort two-dimensional arrays
The PHP language uses the Usort () function to sort two-dimensional arrays:
[][]== a[][1]= length; A[][2] = views Hits

$AAlong = 10; Variable $aalong defines the length of the array
for ($i =0; $i < $AAlong; + + $i)//multidimensional array expansion using ...
//{
for ($j =0; $j < $AAlong; + + $j)//for j22//This segment assigns a value to an 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 show the Array (original) value again
for ($j =0; $j < $AAlong; + + $j)//for j22
{
echo "number". $AA [$j][0]. ", Length". $AA [$j][1];
echo ", its number of hits." $AA [$j][2];
echo "\ r \ n";
}//for j22


Use Usort (array variable, sort function ()); Sort of way!
Usort ($AA, function ($a, $b) {
$al = ($a [2]); Without parentheses, of course, parentheses indicate that this can be an expression, a function, etc...
$BL = $b [2];
if ($al = = $BL)
return 0;
Return ($al > $bl)?  -1:1; This is the positive order (sort), reverse? Write this://return ($al < $BL)?    -1:1; is the reverse!!
});


echo "\ r \ n again show sorted: \ n";
for ($j =0; $j < $AAlong; + + $j)//for j23
{
echo "First". $j. " A: Number ". $AA [$j][0].", Length ". $AA [$j][1];
echo ", its number of hits." $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 guarantees countless yards like me ... Let us borns have food to eat!! :)

A friend said, you forgot to write? > "To end the PHP closed language, I wrote from the habit of Drupal"? > "will be wrong, since do not write"? > "It's not going to go wrong. Must write again when write "? > "Right!!

Php_i love u (1) php Food and clothing parents: Use the Usort () function to sort two-dimensional arrays

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.