分享下PHP数组排序之sort、asort与ksort用法,实例中简单示范了sort、asort与ksort的用法,并备有注释加以详细说明。
PHP数组排序中sort、asort与ksort的用法。
1 2 3 4 5 6 7 8 9 10 11 |
|
本文实例中仅对ksort用法做了示范,大家可以再测试sort与asort的运行结果,以便加深印象,牢固掌握。
不知道你是怎么写的,是少了个planet1,不过不用它也行,就用这个2就可以了,还有就是数组那块分割用逗号,别用分号,语句结束用分号.下面是我改的.自己看吧..
$planet2=array(
'X'=>'Earth',
'Y'=>'Venus',
'Z'=>'Mars',
'A'=>'Jupiter',
'B'=>'Saturn',
);
asort($planet2);
echo '使用函数asort对数组元素排序:';
echo '
';
foreach($planet2 as $key => $value)
{
echo 'planet2['.$key.']='.$value;
echo '
';
echo '
';
}
echo '
';
echo '使用函数ksort对数组元素排序:';
echo '
';
ksort($planet2);
foreach($planet2 as $key=>$value)
{
echo 'planet2['.$key.']='.$value;
echo '
';
echo '
';
}
?>
其实多看看php手册还是有帮助的,LZ所说的正序和倒序是用在什么上?如果是数组上直接使用:
对数组按照键名排序:ksort($array)
对数组按照键名逆向排序:krsort($array)
对数组进行排序并保持索引关系:asort($array)
对数组进行逆向排序并保持索引关系:arsort($array)
对数组逆向排序:rsort
对数组排序:sort