PHP__2014.5.23的总结:
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
2: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
3:ksort:对数组按照键名排序,(升序)
例:<?php
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
ksort( $fruits );
foreach ( $fruits as $key => $val ) {
echo " $key = $val \n" ;
}
?>
以上例程会输出:
a = orange
b = banana
c = apple
d = lemon
4:krsort() 函数将数组按照键逆向排序,为数组值保留原来的键,(降序)
例:<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
krsort($my_array);
print_r($my_array);
?>
以上例程会输出:
Array
(
[c] => Horse
[b] => Cat
[a] => Dog
);
5:sort():对数组的值排序,不会保存键(升序)
例:<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r($my_array);
?>
以上例程会输出:
Array
(
[0] => Cat
[1] => Dog
[2] => Horse
);
6:注意
如果键名重复就会覆盖
例:<?php
$arr=array(
"key"=>"张三"
"key"=>"李四"
);
echo $arr["key"];
输出结果是:历史