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