本帖最后由 baidu_24894285 于 2015-01-26 20:32:24 编辑 定义了一个二维数组,然后自定义了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是什么,打印出来就知道了。
<br />$products = array( array('TIR','tires','100'),<br />array('OIL','oil','10'),<br />array('SPK','spark plugs','4')<br />);<br /><br />function compare1($x,$y){<br /> echo 'x:';<br /> var_dump($x);<br /> echo 'y:';<br /> var_dump($y);<br /><br /> if ($x[1] == $y[1]) {<br /> return 0;<br /> }elseif ($x[1]<$y[1]) {<br /> return -1;<br /> }<br /> else{<br /> return 1;<br /> }<br />}<br /><br />usort($products,'compare1');<br />
Copier après la connexion
<br />x:<br />array (size=3)<br /> 0 => string 'OIL' (length=3)<br /> 1 => string 'oil' (length=3)<br /> 2 => string '10' (length=2)<br />y:<br />array (size=3)<br /> 0 => string 'TIR' (length=3)<br /> 1 => string 'tires' (length=5)<br /> 2 => string '100' (length=3)<br />x:<br />array (size=3)<br /> 0 => string 'SPK' (length=3)<br /> 1 => string 'spark plugs' (length=11)<br /> 2 => string '4' (length=1)<br />y:<br />array (size=3)<br /> 0 => string 'OIL' (length=3)<br /> 1 => string 'oil' (length=3)<br /> 2 => string '10' (length=2)<br />x:<br />array (size=3)<br /> 0 => string 'TIR' (length=3)<br /> 1 => string 'tires' (length=5)<br /> 2 => string '100' (length=3)<br />y:<br />array (size=3)<br /> 0 => string 'OIL' (length=3)<br /> 1 => string 'oil' (length=3)<br /> 2 => string '10' (length=2)<br />x:<br />array (size=3)<br /> 0 => string 'SPK' (length=3)<br /> 1 => string 'spark plugs' (length=11)<br /> 2 => string '4' (length=1)<br />y:<br />array (size=3)<br /> 0 => string 'TIR' (length=3)<br /> 1 => string 'tires' (length=5)<br /> 2 => string '100' (length=3)<br />
Copier après la connexion