These three blog posts demonstrate almost all commonly used array functions. In PHP development, most of the time is spent on operating strings and arrays, so related functions are more important.
http://php.net/manual/zh/ref.array.php
Bubble sort
array_slice() Remove a segment from the array
array_splice() Remove part of the array and replace it with other values
array_combine() Create an array, using the value of one array as its key name and the value of another array as its value
Array Addition and array merging
array_merge() Merge one or more arrays
Intersection and difference
array_intersect() Calculate the intersection of arrays
array_diff() Calculate the difference of arrays
Push and pop
array_push() Push one or more units to the end of the array (push)
array_pop() Pop the last unit of the array (pop)
Queue
array_unshift() Insert one or more cells at the beginning of the array (enqueue)
array_shift() Move the cells at the beginning of the array out of the array
Randomly
array_rand() From the array Randomly take out one or more units (randomly)
Shuffle
shuffle() Shuffle the array
Sum
array_sum() Calculate the sum of all values in the array (Sum)
range() Create an array containing cells in the specified range
array_fill() Fill the array with the given value
<?php /** * 冒泡排序 * * array_slice() 从数组中取出一段 * array_splice() 把数组中的一部分去掉并用其它值取代 * array_combine() 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 * * 数组相加 与 数组合并 * array_merge() 合并一个或多个数组 * * 交集 与 差集 * array_intersect() 计算数组的交集 * array_diff() 计算数组的差集 * * 入栈 与 出栈 * array_push() 将一个或多个单元压入数组的末尾(入栈) * array_pop() 将数组最后一个单元弹出(出栈) * * 队列 * array_unshift() 在数组开头插入一个或多个单元(入队) * array_shift() 将数组开头的单元移出数组 * * 随机 * array_rand() 从数组中随机取出一个或多个单元(随机) * * 打乱 * shuffle() 将数组打乱 * * 求和 * array_sum() 计算数组中所有值的和(求和) * * range() 建立一个包含指定范围单元的数组 * array_fill() 用给定的值填充数组 */ /** * 冒泡排序 */ echo '---------- 冒泡排序 从小到大 ----------<br>'; $arr = array(9,8,7,6,5,4,3,2,1,0); $tmp; for($i=0;$i<count($arr)-1;$i++ ){ for($j=0;$j<count($arr)-1-$i;$j++){ if($arr[$j] > $arr[$j+1]){ $tmp = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $tmp; } } } print_r($arr); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 ) echo '<br>'; echo '---------- 冒泡排序 从大到小 ----------<br>'; $arr = array(0,1,2,3,4,5,6,7,8,9); $tmp; for($i=0;$i<count($arr)-1;$i++ ){ for($j=0;$j<count($arr)-1-$i;$j++){ if($arr[$j+1] > $arr[$j]){ $tmp = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $tmp; } } } print_r($arr); // Array ( [0] => 9 [1] => 8 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 ) echo '<br>'; /** * array_slice() 从数组中取出一段 */ echo '---------- array_slice() ----------<br>'; $arr2 = array("a", "b", "c", "d", "e"); $arr2n = array_slice($arr2, 2, 2); // 从下标2开始,取出2个元素 print_r($arr2n); // Array ( [0] => c [1] => d ) echo '<br>'; $arr2n2 = array_slice($arr2, -2, 2); // 从(最大下标-2)开始,取出2个元素 print_r($arr2n2); // Array ( [0] => d [1] => e ) echo '<br>'; /** * array_splice() 把数组中的一部分去掉并用其它值取代 */ echo '---------- array_splice() ----------<br>'; $arr2 = array("a", "b", "c", "d", "e"); array_splice($arr2, 2); // 从下标2开始,去掉后面所有元素 print_r($arr2); // Array ( [0] => a [1] => b ) echo '<br>'; $arr2 = array("a", "b", "c", "d", "e"); array_splice($arr2, -3, 2); // 从(最大下标-3)开始,去掉2个元素 print_r($arr2); // Array ( [0] => a [1] => b [2] => e ) echo '<br>'; $arr2 = array("a", "b", "c", "d", "e"); array_splice($arr2, -3, 2, "hello"); // 从(最大下标-3)开始,去掉2个元素。从去掉的下标开始添加新元素 print_r($arr2); // Array ( [0] => a [1] => b [2] => hello [3] => e ) echo '<br>'; $arr2 = array("a", "b", "c", "d", "e"); array_splice($arr2, -3, 2, array("hello", "world", "android")); // 从(最大下标-3)开始,去掉2个元素。从去掉的下标开始添加新元素 print_r($arr2); // Array ( [0] => a [1] => b [2] => hello [3] => world [4] => android [5] => e ) echo '<br>'; /** * array_combine() 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 */ echo '---------- array_combine() ----------<br>'; $arrA = array("os", "webserver", "db", "language"); $arrB = array("linux", "apache", "mysql", "php"); $arrC = array_combine($arrA,$arrB); print_r($arrC); // Array ( [os] => linux [webserver] => apache [db] => mysql [language] => php ) echo '<br>'; /** * 数组相加 * 相加时,下标相同时,$arrA的元素会覆盖$arrB的元素 */ echo '---------- 数组相加 ----------<br>'; $arrA = array("os", "webserver", "db", "language"); $arrB = array("linux", "apache", "mysql", "php"); $arrC = $arrA + $arrB; print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language ) echo '<br>'; $arrA = array("os", "webserver", "db", "language"); $arrB = array(9=>"linux", "apache", "mysql", "php"); $arrC = $arrA + $arrB; print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language [9] => linux [10] => apache [11] => mysql [12] => php ) echo '<br>'; /** * 数组合并 * array_merge() 合并一个或多个数组 * 如果下标是字符串,也就是说关联数组,下标相同时,$arrB的元素会覆盖$arrA的元素 */ echo '---------- array_merge() 数组合并 ----------<br>'; $arrA = array("os", "webserver", "db", "language"); $arrB = array("linux", "apache", "mysql", "php"); $arrC = array_merge($arrA, $arrB); print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language [4] => linux [5] => apache [6] => mysql [7] => php ) echo '<br>'; $arrA = array("os", "two"=>"webserver", "db", "language"); $arrB = array("linux", "two"=>"apache", "mysql", "php"); $arrC = array_merge($arrA, $arrB); print_r($arrC); // Array ( [0] => os [two] => apache [1] => db [2] => language [3] => linux [4] => mysql [5] => php ) echo '<br>'; /** * array_intersect() 计算数组的交集 */ echo '---------- array_intersect() 交集 ----------<br>'; $arrA = array("os", "www", "db", "linux"); $arrB = array("linux", "apache", "mysql", "www"); $arrC = array_intersect($arrA, $arrB); print_r($arrC); // Array ( [1] => www [3] => linux ) echo '<br>'; /** * array_diff() 计算数组的差集 */ echo '---------- array_diff() 差集 ----------<br>'; $arrA = array("os", "www", "db", "linux"); $arrB = array("linux", "apache", "mysql", "www"); $arrC = array_diff($arrA, $arrB); print_r($arrC); // Array ( [0] => os [2] => db ) echo '<br>'; /** * array_push() 将一个或多个单元压入数组的末尾(入栈) */ echo '---------- array_push() 入栈 ----------<br>'; $arrZhan = array(); array_push($arrZhan,"one"); print_r($arrZhan); // Array ( [0] => one ) echo '<br>'; array_push($arrZhan,"two"); array_push($arrZhan,"three"); print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three ) echo '<br>'; array_push($arrZhan,"four","five"); print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three [3] => four [4] => five ) echo '<br>'; /** * array_pop() 将数组最后一个单元弹出(出栈) */ echo '---------- array_pop() 出栈 ----------<br>'; array_pop($arrZhan); print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three [3] => four ) echo '<br>'; echo array_pop($arrZhan); // four echo '<br>'; print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three ) echo '<br>'; /** * array_unshift() 在数组开头插入一个或多个单元(入队) */ echo '---------- array_unshift() 入队 ----------<br>'; $arrDuilie = array(); array_unshift($arrDuilie, "one"); array_unshift($arrDuilie, "two"); array_unshift($arrDuilie, "three"); array_unshift($arrDuilie, "four"); print_r($arrDuilie); echo '<br>'; // Array ( [0] => four [1] => three [2] => two [3] => one ) /** * array_shift() 将数组开头的单元移出数组(出队) */ echo '---------- array_shift() 出队 ----------<br>'; array_shift($arrDuilie); print_r($arrDuilie); echo '<br>'; // Array ( [0] => three [1] => two [2] => one ) echo array_shift($arrDuilie); // three echo '<br>'; print_r($arrDuilie); // Array ( [0] => two [1] => one ) echo '<br>'; /** * array_rand() 从数组中随机取出一个或多个单元(随机) */ echo '---------- array_rand() 随机 ----------<br>'; $arrZ = array("a", "b", "c", "d", "e"); var_dump(array_rand($arrZ)); // int(1) echo '<br>'; var_dump(array_rand($arrZ,3)); // array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3) } echo '<br>'; $randValue = array_rand($arrZ); $result = is_string($randValue); // 判断$randValue是不是字符串 var_dump($result); // bool(false) echo '<br>'; /** * shuffle() 将数组打乱 */ echo '---------- shuffle() 打乱 ----------<br>'; $arrZ = array("a", "b", "c", "d", "e"); shuffle($arrZ); print_r($arrZ); //Array ( [0] => c [1] => b [2] => d [3] => e [4] => a ) echo '<br>'; /** * array_sum() 计算数组中所有值的和(求和) */ echo '---------- array_sum() 求和 ----------<br>'; $arrH = array(1,2,3,4,5,6,7,8,9); echo array_sum($arrH); // 45 echo '<br>'; /** * range() 建立一个包含指定范围单元的数组 */ echo '---------- range() 求和 ----------<br>'; $arrR = range(0,10); print_r($arrR); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 ) echo '<br>'; $arrR = range(0,10,3); // 第三个参数表示步进(跳步) print_r($arrR); // Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 ) echo '<br>'; /** * array_fill() 用给定的值填充数组 */ echo '---------- array_fill() 求和 ----------<br>'; $arrF = array_fill(0,5,"iwanghang"); print_r($arrF); // Array ( [0] => iwanghang [1] => iwanghang [2] => iwanghang [3] => iwanghang [4] => iwanghang ) echo '<br>';
The above is how Android programmers learn PHP development ( 25)-Array operation related functions (3) Disassembly and arrangement-the content of PhpStorm, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!