php 数组的常用函数

在php中数组是种强大的数据类型,他可以做的事情很多,可以存储不同的数据类型在一个数组中,下面我们列出了数组常用的操作,排序,键名对数组排序等做法,函数代码如下:

  1. <?php
  2. /* 数组的常用函数
  3. *
  4. * 数组的排序函数
  5. * sort()
  6. * rsort()
  7. * usort()
  8. * asort()
  9. * arsort()
  10. * uasort()
  11. * ksort()
  12. * krsort()
  13. * uksort()
  14. * uatsort()
  15. * natcasesort()
  16. * array_multisort()
  17. *
  18. * 1.简单的数组排序
  19. * sort() rsort()
  20. * 2.根据键名对数组排序
  21. * ksort() krsort()
  22. * 3.根据元素的值对数组排序
  23. * asort() arsort()
  24. * 4.根据"自然数排序“法对数组排序
  25. * natsort()//区分大小写字母比较 natcasescort()//不区分大小写字母的比较
  26. * 5.根据用户自定义规则对数组排序
  27. * usort() uasort() uksort()对键排序
  28. * 6.对维数组的排序
  29. * array_multisort()
  30. *
  31. * 拆分、合并、分解、接合的数组函数
  32. * 1.array_slice()
  33. * 2.array_splice()//删除
  34. * 3.array_combine()//合并
  35. * 4.array_merge();//合并
  36. * 5.array_intersect();//多个数组的交集
  37. * 6.array_diff();//返回多个数组的差集
  38. *
  39. * 数组与数据结构的函数
  40. * 1.使用数组实现堆栈 //先进后出
  41. * array_push() array_pop()
  42. * 2.使用数组实现队列 //先进先出
  43. * array_unshift() array_shift() unset()
  44. *
  45. *
  46. * 其他与数组操作有关的函数
  47. * array_rand()
  48. * shuffle()
  49. * array_sum()
  50. * range()
  51. */
  52. //简单数组排序的使用
  53. $data=array(5,8,1,7,2);
  54. sort($data);//元素由小到大进行排序
  55. print_r($data);//Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )
  56. rsort($data);//元素由大到小进行排序
  57. print_r($data);//Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )
  58. //根据键名排序的例子
  59. $data_2=array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=>"two");
  60. ksort($data_2);//对数组的下标进行由小到大排序
  61. print_r($data_2);//Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )
  62. krsort($data_2);//对数组的下标进行由大到小排序
  63. print_r($data_2);//Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )
  64. //根据元素的值对数组排序
  65. $data_3=array("1"=>"Linux","a"=>"Apache","m"=>"MySQL","l"=>"PHP");
  66. //asort() arsort 与 sort() rsort()的区别在于 前者排序后保持原有的键名,后者不保持原有键名,且键名从0开始
  67. asort($data_3);
  68. print_r($data_3);//Array ( [a] => Apache [1] => Linux [m] => MySQL [l] => PHP )
  69. echo '<br/>';
  70. arsort($data_3);
  71. print_r($data_3);//Array ( [l] => PHP [m] => MySQL [1] => Linux [a] => Apache )
  72. echo '<br/>';
  73. sort($data_3);
  74. print_r($data_3);//Array ( [0] => Apache [1] => Linux [2] => MySQL [3] => PHP )
  75. echo '<br/>';
  76. rsort($data_3);
  77. print_r($data_3);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )
  78. //根据”自然数排序法“对数组排序(0-9短者优先)
  79. $data_4=array("file.txt","file11.txt","file2.txt","file22.txt");
  80. sort($data_4);
  81. print_r($data_4);//Array ( [0] => file.txt [1] => file11.txt [2] => file2.txt [3] => file22.txt )
  82. echo '<br>';
  83. natsort($data_4);
  84. print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
  85. echo '<br>';
  86. natcasesort($data_4);
  87. print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
  88. echo '<br>';
  89. //用户自定义排序函数
  90. echo '<br/>';
  91. $data_5=array("Linux","Apache","MySQL","PHP");
  92. usort($data_5,"sortbylen");//通过元素长度排序
  93. print_r($data_5);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )
  94. function sortbylen($one,$two){
  95. if(strlen($one)==strlen($two))
  96. return 0;
  97. else
  98. return (strlen($one)>strlen($two))?1:-1;
  99. }
  100. //拆分、合并、分解、接合的数组函数
  101. echo '<br/>';
  102. $data_6=array("Linux","Apache","MySQL","PHP");
  103. print_r(array_slice($data_6,1,2));//取下标为1、2的元素
  104. //Array ( [0] => Apache [1] => MySQL ) 下标重置从0开始
  105. echo '<br/>';
  106. print_r(array_slice($data_6,-2,1));//从后边的第二个开始取返回一个,不是从0开始的
  107. //Array ( [0] => MySQL ) 下标重置从0开始
  108. echo '<br/>';
  109. print_r(array_slice($data_6,1,2,true));
  110. //Array ( [1] => Apache [2] => MySQL ) 保留原有的下标
  111. echo '<br/>';
  112. //array_combine()
  113. $a1=array("OS","WebServer","DataBase","Language");
  114. $a2=array("Linux","Apache","MySQL","PHP");
  115. print_r(array_combine($a1,$a2));//第一个参数作为键名,第二个作为值来合并
  116. //Array ( [OS] => Linux [WebServer] => Apache [DataBase] => MySQL [Language] => PHP )
  117. echo '<br/>';
  118. //array_merge()
  119. $a3=array("OS","WebServer","DataBase","Language");
  120. $a4=array("Linux","Apache","MySQL","PHP");
  121. $a5=$a3+$a4;
  122. print_r($a5);//因为两个数组下标重复所以显示这样
  123. //Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language )
  124. echo '<br/>';
  125. print_r(array_merge($a3,$a4));//合并并重新索引
  126. //Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP )
  127. echo '<br/>';
  128. //array_intersect()
  129. $a7=array("OS","WebServer","DataBase","Language",1,2,3);
  130. $a8=array("Linux","Apache","MySQL","PHP",2,3,4);
  131. print_r(array_intersect($a7,$a8));//Array ( [5] => 2 [6] => 3 )
  132. echo '<br/>';
  133. //array_diff()
  134. $a9=array(1,2,3,4);
  135. $a10=array(3,4,5,6);
  136. print_r(array_diff($a9,$a10));//Array ( [0] => 1 [1] => 2 )
  137. //返回第一个数组跟第二个相差的元素
  138. echo '<br/>';
  139. //使用数组实现堆栈
  140. $b=array(1,2,3,4);
  141. $b[]="a";//入栈
  142. array_push($b,"b","c");//使用函数入栈
  143. print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b [6] => c )
  144. echo '<br/>';
  145. $value=array_pop($b);//使用函数出栈
  146. print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b )
  147. echo '<br/>';
  148. echo $value;//显示出栈的元素的值 c
  149. echo '<br/>';
  150. //使用数组实现队列
  151. $c=array(1,2,3);
  152. print_r($c);//Array ( [0] => 1 [1] => 2 [2] => 3 )
  153. echo '<br/>';
  154. array_unshift($c,"abc","bcd");//入队
  155. print_r($c);//Array ( [0] => abc [1] => bcd [2] => 1 [3] => 2 [4] => 3 )
  156. echo '<br/>';
  157. $values=array_shift($c);//出队
  158. print_r($c);// Array ( [0] => bcd [1] => 1 [2] => 2 [3] => 3 )
  159. echo '<br/>';
  160. unset($c[2]);//删除指定位置元素
  161. print_r($c);//Array ( [0] => bcd [1] => 1 [3] => 3 )
  162. echo '<br/>';
  163. //array_rand() 随机返回数组下标
  164. $arr=array(1,3,4,5,76,7,99,6,2,3);
  165. echo array_rand($arr);//返回的是随机的数组元素的下标
  166. echo $arr[array_rand($arr)];//随机显示数组元素的值
  167. echo '<br/>';
  168. //shuffle() 随机重新排列数组
  169. $arr2=array(32,35,33);
  170. shuffle($arr2);
  171. print_r($arr2);//数组元素位置随机变换
  172. echo '<br/>';
  173. //array_sum() 求和
  174. $arr3=array(1,3,5);
  175. echo array_sum($arr3); //返回9
  176. echo '<br/>';
  177. print_r($arr3);//Array ( [0] => 1 [1] => 3 [2] => 5 )
  178. echo '<br/>';
  179. //range(最小值,最大值,步长)
  180. $arr4=range(0,100,10);
  181. print_r($arr4);//Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )
  182. //开源代码phpfensi.com
  183. ?>