This code does not understand how to sort arrays.

Source: Internet
Author: User
This code does not understand how to sort arrays. At the end of this post, a two-dimensional array is edited and defined by baidu_24894285 at 2015-01-2620: 32: 24, and two functions are customized, the first one sorts the second column in the array in ascending order of letters, and the second function sorts the third column in the array in ascending order of numbers. What I don't understand is that these two UDFs are so strange that I don't understand this code.

This post was last edited by baidu_24894285 on 20:32:24

Defines a two-dimensional array, and then customizes two functions. The first one sorts the second column in the array in ascending order of letters, and the second function sorts the third column in the array.
The numbers are listed in ascending order.
What I don't understand is how strange these two UDFs are. what does $ x $ y in them represent? how can we compare them with others? Which of the following heroes can explain in detail the logical structure.

$ Products = array ('tire', 'tires', '123 '),
Array ('oil ', 'Oil', '10 '),
Array ('spk', 'spark S s', '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, 'company1 ');
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, 'company2 ');
Var_dump ($ products );
?>


Print result:
Array (size = 3)
0 =>
Array (size = 3)
0 => string 'core' (length = 3)
1 => string 'tires' (length = 5)
2 => string '20140901' (length = 3)
1 =>
Array (size = 3)
0 => string 'oil '(length = 3)
1 => string 'oil '(length = 3)
2 => string '10' (length = 2)
2 =>
Array (size = 3)
0 => string 'spk' (length = 3)
1 => string 'spark S s' (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 '10' (length = 2)
1 =>
Array (size = 3)
0 => string 'spk' (length = 3)
1 => string 'spark S s' (length = 11)
2 => string '4' (length = 1)
2 =>
Array (size = 3)
0 => string 'core' (length = 3)
1 => string 'tires' (length = 5)
2 => string '20140901' (length = 3)

Array (size = 3)
0 =>
Array (size = 3)
0 => string 'spk' (length = 3)
1 => string 'spark S s' (length = 11)
2 => string '4' (length = 1)
1 =>
Array (size = 3)
0 => string 'oil '(length = 3)
1 => string 'oil '(length = 3)
2 => string '10' (length = 2)
2 =>
Array (size = 3)
0 => string 'core' (length = 3)
1 => string 'tires' (length = 5)
2 => string '20140901' (length = 3)

------ Solution ----------------------
Take a look at the second parameter description of the usort method in the manual:
Cmp_function
When the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0.
Int callback (mixed $ a, mixed $ B)

It is easy to understand that $ a and $ B are the elements currently used for comparison in the array. In your example
Array ('tire', 'tires', '123 ')
Array ('oil ', 'Oil', '10 '),
Array ('spk', 'spark S s', '4 ')

In fact, if you want to know what $ a and $ B are, print them out.

$products = array( array('TIR','tires','100'),
array('OIL','oil','10'),
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 '10' (length=2)
y:
array (size=3)
0 => string 'TIR' (length=3)
1 => string 'tires' (length=5)
2 => string '100' (length=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 => string '10' (length=2)
x:
array (size=3)
0 => string 'TIR' (length=3)
1 => string 'tires' (length=5)
2 => string '100' (length=3)
y:
array (size=3)
0 => string 'OIL' (length=3)
1 => string 'oil' (length=3)
2 => string '10' (length=2)
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 'TIR' (length=3)
1 => string 'tires' (length=5)
2 => string '100' (length=3)

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.