PHP数组_5_3_数组处理函数及其应用_7_数组排列函数
作者:互联网
以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。
数组排列函数
1. sort() 函数
程序:
1 <?php 2 $array = array("img12.gif","img10.gif","img2.gif","img1.gif"); 3 sort($array); 4 print_r($array); 5 //Array ( [0] => img1.gif [1] => img10.gif [2] => img12.gif [3] => img2.gif ) 6 ?>
输出:
Array ( [0] => img1.gif [1] => img10.gif [2] => img12.gif [3] => img2.gif )
2. asort() 函数
程序:
1 <?php 2 $array = array("img12.gif","img10.gif","img2.gif","img1.gif"); 3 asort($array); 4 print_r($array); 5 //Array ( [3] => img1.gif [1] => img10.gif [0] => img12.gif [2] => img2.gif ) 6 ?>
输出:
Array ( [3] => img1.gif [1] => img10.gif [0] => img12.gif [2] => img2.gif )
3. rsort() 函数 和 arsort() 函数
程序:
1 <?php 2 $array1 = $array2 = array("img12.gif","img10.gif","img2.gif","img1.gif"); 3 rsort($array1); 4 print_r($array1); 5 //Array ( [0] => img2.gif [1] => img12.gif [2] => img10.gif [3] => img1.gif ) 6 echo "<br/>"; 7 arsort($array2); 8 print_r($array2); 9 //Array ( [2] => img2.gif [0] => img12.gif [1] => img10.gif [3] => img1.gif ) 10 ?>
输出:
Array ( [0] => img2.gif [1] => img12.gif [2] => img10.gif [3] => img1.gif ) Array ( [2] => img2.gif [0] => img12.gif [1] => img10.gif [3] => img1.gif )
4. ksort() 函数
程序:
1 <?php 2 $array1 = array( "c"=>"China","f"=>"French","e"=>"English" ); 3 ksort($array1); 4 print_r($array1); 5 //Array ( [c] => China [e] => English [f] => French ) 6 ?>
输出:
Array ( [c] => China [e] => English [f] => French )
5. krsort() 函数
程序:
1 <?php 2 $array1 = array( "c"=>"China","f"=>"French","e"=>"English" ); 3 krsort($array1); 4 print_r($array1); 5 //Array ( [f] => French [e] => English [c] => China ) 6 ?>
输出:
Array ( [f] => French [e] => English [c] => China )
6. natsort() 函数
程序:
1 <?php 2 $array = array("A12","a10","a2","A1"); 3 natsort($array); //区分大小写 4 print_r($array); 5 //Array ( [3] => A1 [0] => A12 [2] => a2 [1] => a10 ) 6 ?>
输出:
Array ( [3] => A1 [0] => A12 [2] => a2 [1] => a10 )
7. natcasesort() 函数
程序:
1 <?php 2 $array = array("A12","a10","a2","A1"); 3 natcasesort($array); //不区分大小写 4 print_r($array); 5 //Array ( [3] => A1 [2] => a2 [1] => a10 [0] => A12 ) 6 ?>
输出:
Array ( [3] => A1 [2] => a2 [1] => a10 [0] => A12 )
8. shuffle() 函数
程序:
1 <?php 2 $array = array("A12","a10","a2","A1"); 3 shuffle($array); 4 print_r($array); //Array ( [0] => a10 [1] => a2 [2] => A1 [3] => A12 ) 5 echo "<br/>"; 6 shuffle($array); 7 print_r($array); //Array ( [0] => A1 [1] => a10 [2] => a2 [3] => A12 ) 8 echo "<br/>"; 9 ?>
输出:
Array ( [0] => a10 [1] => a2 [2] => A1 [3] => A12 ) Array ( [0] => A1 [1] => a10 [2] => a2 [3] => A12 )
9. array_reverse() 函数
程序:
1 <?php 2 $array = array("a12","a1","a2","a10"); 3 $newArray1 = array_reverse($array); 4 print_r($newArray1); //Array ( [0] => a10 [1] => a2 [2] => a1 [3] => a12 ) 5 echo "<br/>"; 6 $newArray2 = array_reverse($array,TRUE); 7 print_r($newArray2); //Array ( [3] => a10 [2] => a2 [1] => a1 [0] => a12 ) 8 echo "<br/>"; 9 ?>
输出:
Array ( [0] => a10 [1] => a2 [2] => a1 [3] => a12 ) Array ( [3] => a10 [2] => a2 [1] => a1 [0] => a12 )
标签:a10,gif,a2,数组,img2,Array,PHP,处理函数,img1 来源: https://www.cnblogs.com/xiaoxuStudy/p/11827585.html