sort 1 sort — 对数组排序
本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。
2
Example #1 sort()
例子
<?php
$fruits
= array( "lemon" ,
"orange" , "banana"
, "apple" );
sort sort(
$fruits );
foreach (
$fruits as $key
=> $val
) {
echo
"fruits[" . $key
. "] = " .
$val . "\n"
;
}
?>
以上例程会输出:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
fruits 被按照字母顺序排序。
3 rsort — 对数组逆向排序
本函数对数组进行逆向排序(最高到最低)。
Example #1 rsort()
例
<?php
$fruits
= array( "lemon" ,
"orange" , "banana"
, "apple" );
rsort rsort(
$fruits );
foreach (
$fruits as $key
=> $val
) {
echo "
$key = $val \n"
;
}
?>
以上例程会输出:
0 = orange
1 = lemon
2 = banana
3 = apple
fruits 被按照字母顺序逆向排序。
4 ksort— 对数组按照键名排序
对数组按照键名排序,保留键名到数据的关联。本函数主要用于关联数组。
Example #1 ksort()
例子
<?php
$fruits
= array( "d" =>
"lemon" , "a" =>
"orange" , "b" =>
"banana" , "c" =>
"apple" );
ksort ksort(
$fruits );
foreach (
$fruits as $key
=> $val
) {
echo "
$key = $val \n"
;
}
?>
以上例程会输出:
a = orange
b = banana
c = apple
d = lemon
5 krsort— 对数组按照键名逆向排序
对数组按照键名逆向排序,保留键名到数据的关联。主要用于结合数组。
Example #1 krsort()
例子
<?php
$fruits
= array( "d" =>
"lemon" , "a" =>
"orange" , "b" =>
"banana" , "c" =>
"apple" );
krsort krsort(
$fruits );
foreach (
$fruits as $key
=> $val
) {
echo "
$key = $val \n"
;
}
?>
以上例程会输出:
d = lemon
c = apple
b = banana
a = orange
6 asort — 对数组进行排序并保持索引关系
本函数对数组进行排序,数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。
Example #1 asort()
例子
<?php
$fruits
= array( "d" =>
"lemon" , "a"
=> "orange" ,
"b" => "banana"
, "c" =>
"apple" );
asort (
$fruits );
foreach (
$fruits as $key
=> $val
) {
echo "
$key = $val \n"
;
}
?>
以上例程会输出:
c = apple
b = banana
d = lemon
a = orange
fruits 被按照字母顺序排序,并且单元的索引关系不变
7 arsort— 对数组进行逆向排序并保持索引关系
本函数对数组进行排序,数组的索引保持和单元的关联。
主要用于对那些单元顺序很重要的结合数组进行排序。
Example #1 arsort()
例子
<?php
$fruits
= array( "d" =>
"lemon" , "a"
=> "orange" ,
"b" => "banana"
, "c" =>
"apple" );
arsort
( $fruits );
foreach (
$fruits as $key
=> $val
) {
echo "
$key = $val \n"
;
}
?>
以上例程会输出:
a = orange
d = lemon
b = banana
c = apple
fruits 被按照字母顺序逆向排序,并且单元的索引关系不变。