015——数组(十五)sort natsort shuffle natcasesoft array_multisort

<?php
/*数组排序函数
 * sort natsort shuffle natcasesoft array_multisort
 */

//sort() 对数组元素进行递增的排序,
/*$arr=array(10,2,3,40,5);
sort($arr);
print_r($arr);//输出:Array ( [0] => 2 [1] => 3 [2] => 5 [3] => 10 [4] => 40 )*/

/*$arr2=array('class1','class2','class12','class32','class21');
sort($arr2);
print_r($arr2);//输出:Array ( [0] => class1 [1] => class12 [2] => class2 [3] => class21 [4] => class32 )*/

//natsort() 通过自然排序对数组进行排序。保留原数组索引
/*$arr2=array('class1','class2','class12','class32','class21');
natsort($arr2);
print_r($arr2);//输出:Array ( [0] => class1 [1] => class2 [2] => class12 [4] => class21 [3] => class32 )*/

//不区分字符大小写进行排序:
/*$arr=array('a','b','c','d','A','D','C','B');
natcasesort($arr);
print_r($arr);//输出:Array ( [0] => a [4] => A [1] => b [7] => B [2] => c [6] => C [3] => d [5] => D )*/


//shuffle()随机对数组进行排序  生成新的数组,原有键名删除
/*$arr3=array(1,2,3);
shuffle($arr3);
print_r($arr3);//输出:随机输出,原有键名删除*/

/*$arr3=array(1,2,3);
shuffle($arr3);
foreach ($arr3 as $k=>$v):
    echo $k."=>".$v."<br/>";
endforeach;*/

//array_multisort() 对多个数组或多维数组进行排序,
/*$arr=array(2,1,3,1);
$arr2=array(6,5,4,7);
array_multisort($arr,SORT_ASC,$arr2,SORT_DESC);
print_r($arr);
echo "<br/>=================<br/>";
print_r($arr2);*/
/*
Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 )
=================
Array ( [0] => 7 [1] => 5 [2] => 6 [3] => 4 )
 * */

$arr=array(
    array(2,1,3,1),
    array(6,5,4,7)
);
array_multisort($arr[0],SORT_ASC,$arr[1],SORT_DESC);
print_r($arr);
//输出:Array ( [0] => Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 ) [1] => Array ( [0] => 7 [1] => 5 [2] => 6 [3] => 4 ) )

  

posted @ 2017-11-24 06:52  生如逆旅,一苇以航  阅读(175)  评论(0编辑  收藏  举报