码迷,mamicode.com
首页 > 编程语言 > 详细

区分排序函数

时间:2016-02-29 12:06:17      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

<?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‘);

区分排序函数

标签:

原文地址:http://www.cnblogs.com/zc123/p/5226707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!