This piece of code can not understand, about the array sort.

Source: Internet
Author: User
Tags array sort
Defines a two-dimensional array, then customizes 2 functions, the first arranges the second column in the array in ascending alphabetical order, and the second function adds the third column in the array
The numbers are sorted in ascending order.
What I don't understand is that these two custom functions are very strange ah, the inside of the $x $y represents what ah, not touch the brain, how to compare? I hope that the hero can explain the logical structure in detail.

$products = Array (' TIR ', ' tires ', ' 100 '),
Array (' oil ', ' oil ', ' 10 '),
Array (' SPK ', ' spark plugs ', ' 4 ')
);


Var_dump ($products);

function Compare1 ($x, $y) {
if ($x [1] = = $y [1]) {
return 0;
}elseif ($x [1]< $y [1]) {
return-1;
}
else{
return 1;
}
}

Usort ($products, ' compare1 ');
Var_dump ($products);

function Compare2 ($x, $y) {
if ($x [2] = = $y [2]) {
return 0;
}elseif ($x [2]< $y [2]) {
return-1;
}
else{
return 1;
}
}

Usort ($products, ' compare2 ');
Var_dump ($products);
?>


Printing results:
Array (size=3)
0 =
Array (size=3)
0 = String ' TIR ' (length=3)
1 = String ' tires ' (length=5)
2 = String ' + ' (length=3)
1 =
Array (size=3)
0 = String ' oil ' (length=3)
1 = String ' oil ' (length=3)
2 = String ' Ten ' (length=2)
2 =
Array (size=3)
0 = String ' SPK ' (length=3)
1 = String ' spark plugs ' (length=11)
2 = String ' 4 ' (length=1)

Array (size=3)
0 =
Array (size=3)
0 = String ' oil ' (length=3)
1 = String ' oil ' (length=3)
2 = String ' Ten ' (length=2)
1 =
Array (size=3)
0 = String ' SPK ' (length=3)
1 = String ' spark plugs ' (length=11)
2 = String ' 4 ' (length=1)
2 =
Array (size=3)
0 = String ' TIR ' (length=3)
1 = String ' tires ' (length=5)
2 = String ' + ' (length=3)

Array (size=3)
0 =
Array (size=3)
0 = String ' SPK ' (length=3)
1 = String ' spark plugs ' (length=11)
2 = String ' 4 ' (length=1)
1 =
Array (size=3)
0 = String ' oil ' (length=3)
1 = String ' oil ' (length=3)
2 = String ' Ten ' (length=2)
2 =
Array (size=3)
0 = String ' TIR ' (length=3)
1 = String ' tires ' (length=5)
2 = String ' + ' (length=3)


Reply to discussion (solution)

Take a look at the second parameter description of the Usort method of the manual:
cmp_function
When the first parameter is less than, equal to, or greater than the second argument, the comparison function must return an integer less than, equal to, or greater than 0.
int callback (mixed $a, mixed $b)

Well understood, $a, $b is the element that the array is currently used for comparison. In your case,
Array (' TIR ', ' tires ', ' 100 ')
Array (' oil ', ' oil ', ' 10 '),
Array (' SPK ', ' spark plugs ', ' 4 ')

Actually want to know a $ A, $b is what, printed out to know.

$products = Array ('  TIR ', ' tires ', ' + '), array (' oil ', ' oil ', ' ') ', Array (' SPK ', ' spark plugs ', ' 4 ')); function Compare1 ($x, $y) {    echo ' x: ';    Var_dump ($x);    Echo ' y: ';    Var_dump ($y);    if ($x [1] = = $y [1]) {        return 0;    } ElseIf ($x [1]< $y [1]) {        return-1;    }        else{    return 1;}    } Usort ($products, ' compare1 ');


X:array (size=3)  0 = string ' oil ' (length=3)  1 = string ' oil ' (length=3)  2 = String ' (length=2 ) Y:array (size=3)  0 = String ' TIR ' (length=3)  1 = string ' tires ' (length=5)  2 = String ' (Leng th=3) x:array (size=3)  0 = String ' SPK ' (length=3)  1 = = String ' spark plugs ' (length=11)  2 = string ' 4 ' (length=1) Y:array (size=3)  0 = string ' oil ' (length=3)  1 = string ' oil ' (length=3)  2 = Strin  G ' (length=2) x:array (size=3)  0 = String ' TIR ' (length=3)  1 = string ' tires ' (length=5)  2 = String ' length=3 ' Y:array (size=3)  0 = string ' oil ' (length=3)  1 = string ' oil ' (length=3)  2 = = String ' (length=2) x:array (size=3)  0 = String ' SPK ' (length=3)  1 = string ' spark plugs ' (lengt h=11)  2 = String ' 4 ' (length=1) Y:array (size=3)  0 = String ' TIR ' (length=3)  1 = string ' tires ' (length=5)  2 = String ' + ' (length=3)

Thank you upstairs, I studied for half a day and figured it out.
To be bluntly, this is not a PHP code, it is pseudo-code. So I got confused.

  • 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.