定义了一个二维数组,然后自定义了2个函数,第一个把数组中的第二列按照字母升序进行排列,第二个函数将数组中的第三列
按照数字进行了升序排列。
我看不明白的就是这两个自定义函数好奇怪啊,里面的$x $y代表的是啥啊,摸不着头脑,怎么就比较了?希望哪位大侠能详细讲讲逻辑结构。
$products = array( 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] 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] return -1;
}
else{
return 1;
}
}
usort($products,'compare2');
var_dump($products);
?>
打印结果:
array (size=3)
0 =>
array (size=3)
0 => string 'TIR' (length=3)
1 => string 'tires' (length=5)
2 => string '100' (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 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 '10' (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 '100' (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 '10' (length=2)
2 =>
array (size=3)
0 => string 'TIR' (length=3)
1 => string 'tires' (length=5)
2 => string '100' (length=3)
看看手册的usort方法的第二个参数说明:
cmp_function
在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。
int callback ( mixed $a , mixed $b )
很好理解,$a,$b就是数组当前用于比较的元素。你例子中的
array('TIR','tires','100')
array('OIL','oil','10'),
array('SPK','spark plugs','4')
其实想知道$a,$b是什么,打印出来就知道了。
$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)
谢谢楼上,我研究了半天,弄明白了。
说穿了,这不是php代码,是伪代码。所以把我弄糊涂了。