<?php
//1.sort() 索引数组按照值的字母升序排
$fruits = array( "lemon" , "orange" , "banana" , "apple" );
sort ( $fruits );
foreach ( $fruits as $key => $val ) {
echo "fruits[" . $key . "] = " . $val . "\n" ;
}
echo ‘<hr/>‘;//a、b、o、l
//2.asort()(注:关联、索引)数组按照值的字母排序
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
asort ( $fruits );
foreach ( $fruits as $key => $val ) {
echo " $key = $val \n" ;
}
echo ‘<hr/>‘;//apple、banana、lemon、orange
注:当数组是索引数组时使用asort的排序结果和sort一样但是asort索引会保持排序前的顺序
//3.ksort()按照索引来字母升序排
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
ksort ( $fruits );
foreach ( $fruits as $key => $val ) {
echo " $key = $val \n" ;
}
//array(‘a‘=>‘orange‘,‘b‘=>‘banana‘,‘c‘=>‘apple‘,‘d‘=>‘lemon‘);