These 3 posts demonstrate that almost all commonly used array functions, in PHP development, most of the time is the manipulation of strings and arrays, so the correlation function is more important.
http://php.net/manual/zh/ref.array.php
Bubble sort
Array_slice () Remove a paragraph from the array
Array_splice () Remove part of the array and replace it with other values
Array_combine () creates an array with the value of an array as its key name, and the value of another array as its value
Array addition and array merging
Array_merge () merging one or more arrays
Intersection and Difference set
Array_intersect () computes the intersection of an array
Array_diff () computes the difference set of an array
into the stack and out of the stack
Array_push () presses one or more cells into the end of the array (into the stack)
Array_pop () POPs the last cell of the array (out of the stack)
Queue
Array_unshift () inserts one or more cells at the beginning of the array (queued)
Array_shift () Moves the cell at the beginning of the array to a group
Random
Array_rand () randomly extracts one or more cells from an array (random)
Upset
Shuffle () disrupts the array
Sum
Array_sum () computes sum (sum) of all values in the array
Range () creates an array containing the specified range of cells
Array_fill () fills an array with the given value
<?php/** * Bubble Sort * * array_slice () take a paragraph from the array * Array_splice () Remove part of the array and replace with other values * Array_combine () Create an array, use the value of an array as its key name, and the value of another array as its value * * Array addition to the array merge * Array_merge () Merge one or more arrays * intersection and difference set * Array_int Ersect () computes the intersection of the array * Array_diff () computes the difference set of the array * into and out of the stack * Array_push () presses one or more cells into the end of the array (into the stack) * ARRAY_POP () will The last unit of the array pops up (out of the stack) * * Queue * ARRAY_UNSHIFT () inserts one or more cells at the beginning of the array (queued) * Array_shift () moves the cell at the beginning of the array to the group * random * Array_rand () randomly remove one or more cells from the array (random) * * disturb * shuffle () shuffle the array * * sum * array_sum () calculate sum (sum) of all values in the array * * Range () create an array containing the specified range of cells * Array_fill () fills the array with the given values *//** * Bubble sort */echo '----------bubble sort From small to large----------<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)-$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 '----------bubble sort from big to small----------<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)-$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 () take a paragraph from the array */echo '----------array_slice ()----------<br> '; $arr 2 = Array ("A", "B", "C", "D", "E"); $arr 2n = Array_slice ($arr 2, 2, 2); Starting from subscript 2, remove 2 elements print_r ($arr 2n); Array ([0] = + c [1] + D) echo ' <br> '; $arr 2n2 = Array_slice ($arr 2,-2, 2); Starting from (maximum subscript-2), remove 2 elements print_r ($arr 2n2); Array ([0] = d [1] + E) echo ' <br> '; /** * Array_splice () Remove part of the array and replace with other values */Echo '----------Array_splice ()----------<br> '; $arr 2 = Array ("A", "B", "C", "D", "E"); Array_splice ($arr 2, 2); Starting with subscript 2, remove all subsequent elements Print_r ($arr 2); Array ([0] = a [1] = b) echo ' <br> '; $arr 2 = Array ("A", "B", "C", "D", "E"); Array_splice ($arr 2,-3, 2); Starting with (maximum subscript-3), remove 2 elements Print_r ($arr 2); Array ([0] = a [1] = b [2] = + E) echo ' <br> '; $arr 2 = Array ("A", "B", "C", "D", "E"); Array_splice ($arr 2, -3, 2, "Hello"); Starting with (maximum subscript-3), remove the 2 elements. Add a new element from the dropped subscript print_r ($arr 2); Array ([0] = a [1] = b [2] = = Hello [3] + E) echo ' <br> '; $arr 2 = Array ("A", "B", "C", "D", "E"); Array_splice ($arr 2, -3, 2, Array ("Hello", "World", "Android")); From (max subscript-3)Start by removing 2 elements. Add a new element from the dropped subscript print_r ($arr 2); Array ([0] = a [1] = b [2] = = Hello [3] = world [4] = Android [5] + E) echo ' <br> '; /** * Array_combine () creates an array with the value of an array as its key name, and the value of the other array as its value */echo '----------array_combine ()----------<BR&G t; '; $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> '; /** * When the array is added together, the subscript is the same, $arrA element overrides the $ARRB element/echo '----------array----------<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 [ten] = Apache [one] = m Ysql [+] + php) echo ' <br> '; /** * Array Merge * Array_merge () Merge one or more arrays * If the subscript is a string, that is, an associative array, and the subscript is the same, $arrB element will overwrite the element of the $arra */Echo '---------- Array_merge () array merging----------<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] = = Mys QL [7] = php) echo ' <br> '; $arrA = Array ("OS", "both" = "webserver", "db", "language"); $arrB = Array ("Linux", "both" = "Apache", "MySQL", "PHP"); $arrC = Array_merge ($arrA, $arrB); Print_r ($arrC); Array ([0] = OS [both] = Apache [1] = = DB [2] => Language [3] = = Linux [4] = + MySQL [5] + php) echo ' <br> '; /** * Array_intersect () computes the intersection of the array */echo '----------Array_intersect () intersection----------<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 () calculates the difference set of arrays */echo '----------Array_diff () difference set----------<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 () presses one or more cells into the end of the array (into the stack) */echo '----------Array_push () into the stack----------<br> '; $arrZhan = Array (); Array_push ($arrZhan, "one"); Print_r ($arrZhan); Array ([0] = one) echo ' <br> '; Array_push ($arrZhan, "both"); Array_push ($arrZhan, "three"); Print_r ($arrZhan); Array ([0] = one [1] = [2] = + three) echo ' <br> '; Array_push ($arrZhan, "Four", "five"); Print_r ($arrZhan); Array ([0] = one [1] = [2] = three [3] = four [4] = five) echo ' <br> '; /** * Array_pop () pops the last element of the array (out of the stack)/echo '----------Array_pop () out----------<br> '; Array_pop ($arrZhan); Print_r ($arrZhan); Array ([0] = one [1] = [2] = three [3] = four) echo ' <br> '; echo Array_pop ($arrZhan); Four echo ' <br> '; Print_r ($arrZhan); Array ([0] = one [1] = [2] = + three) echo ' <br> '; /** * Array_unshift () Insert one or more cells at the beginning of the array (queue)/echo '----------array_unshift ()----------<br> '; $arrDuilie = Array (); Array_unshift ($arrDuilie, "one"); Array_unshift ($arrDuilie, "both"); Array_unshift ($arrDuilie, "three"); Array_unshift ($arrDuilie, "four "); Print_r ($arrDuilie); Echo ' <br> '; Array ([0] = four [1] = three [2] = [3] + one)/** * Array_shift () move the cell at the beginning of the array (out of the queue) */ Echo '----------Array_shift () team----------<br> '; Array_shift ($arrDuilie); Print_r ($arrDuilie); Echo ' <br> '; Array ([0] = three [1] = [2] = one) echo array_shift ($arrDuilie); Three echo ' <br> '; Print_r ($arrDuilie); Array ([0] = [1] = one) echo ' <br> '; /** * Array_rand () randomly extracts one or more cells from the array (random) */echo '----------Array_rand () random----------<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); Judge $randvalue is not a string var_dump ($result); BOOL (false) echo ' <bR> '; /** * Shuffle () disturb the array */echo '----------shuffle () to disrupt----------<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 () calculates the sum (sum) of all values in the array */echo '----------array_sum () summation----------<br> '; $arrH = Array (1,2,3,4,5,6,7,8,9); echo Array_sum ($arrH); Echo ' <br> '; /** * Range () creates an array containing the specified range of cells */Echo '----------range () sum----------<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 [Ten] + +) echo ' <br> '; $arrR = range (0,10,3); The third parameter indicates step (skip) Print_r ($arrR); Array ([0] = 0 [1] = 3 [2] = 6 [3] = 9) echo ' <br> '; /** * Array_fill () fills the array with the given value */echo '----------Array_filL () sum----------<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 the Android programmer to learn PHP development (25)-array operation related functions (3) split the arrangement of the contents of the-phpstorm, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!