升序:从低到高
降序:从高到低
sort()
对数组按照值进行升序排序,将会删除原有的键名,并重新赋予新的建名
rsort()
对数组按照值进行降序排序,将会删除原有的键名,并重新赋予新的建名
ksort()
对数组按照键名进行升序排序,保留原有的建名跟值的对应关系
krsort()
对数组按照键名进行降序排序,保留原有的建名跟值的对应关系
asort()
对数组按照值进行升序排序,保留原有的建名跟值的对应关系
arsort()
对数组按照值进行降序排序,保留原有的建名跟值的对应关系
示例:
<?php
//create the movies
$movies=array(
‘gangtiexia‘ =>10,
‘jianbingxia‘=>10,
‘zhuzhuxia‘=>6,
‘doupo‘=>9,
‘dota‘=>7
);
//初始数组
echo ‘<tr><td><b>original order</b></td></tr><br>‘;
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
echo "<hr>";
//ksort()排序
echo ‘<tr><td ><b>ksort by title</b></td></tr><br>‘;
ksort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
//krsort()排序
echo ‘<tr><td ><b>krsort by title</b></td></tr><br>‘;
krsort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
echo "<hr>";
//asort()排序
echo ‘<tr><td><b>asort by grade</b></td></tr><br>‘;
asort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
//arsort()排序
echo ‘<tr><td><b>arsort by grade</b></td></tr><br>‘;
arsort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
echo "<hr>";
//sort()排序
echo ‘<tr><td><b>sort by </b></td></tr><br>‘;
sort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
//rsort()排序
echo ‘<tr><td><b>rsort by </b></td></tr><br>‘;
rsort($movies);
foreach ($movies as $title => $grade) {
# code...
echo "<tr><td>$grade</td>
<td>$title</td></tr><br>";
}
?>
结果:
sort()&rsort()&ksort()&asort()&arsort()&krsort()
时间: 2024-10-29 00:01:01