区分排序函数

//1.sort() 索引数组按照值的字母升序排
$fruits = array( "lemon" , "orange" , "banana" , "apple" );
sort ( $fruits );
foreach ( $fruits as $key => $val ) 
{
  echo "fruits[" . $key . "] = " . $val . "\n" ;
}
echo '<hr/>';//apple、banana、orange、lemon
//2.asort()(注:关联、索引)数组按照值的字母排序 
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
asort ( $fruits ); 
foreach ( $fruits as $key => $val ) 
{
   echo " $key = $val \n" ; 
} //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');

  

posted @ 2016-02-29 10:44  东歌  阅读(186)  评论(0编辑  收藏  举报