<?php/** * 1, the Write function creates an array of length 10. The elements in the array are incremented by an odd number. The first item is 1. */$ary = range (1, ten);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] =& Gt 8 [8] = 9 [9] = Print_r ($ary);/** * 2, create an array of length 10, the elements in the array are increments of equal numbers, the ratio is 3. The first item is 1 */function myfun ($n) {return POW (3, $n-1);} $ary = Range (1), $result = Array_map (' myfun ', $ary); Echo ' <br> ';//array ([0] = 1 [1] = 3 [2] = 9 [3] [4] = Bayi [5] = 243 [6] = 729 [7] = 2187 [8] = 6561 [9] = 19683) print_r ($result);/** * 3 , the subscript of the largest number in the array; * $arr = Array (1,5,67,8,4,3,45,6,887,2); */$arr = array (1,5,67,8,4,3,45,6,887,2), $max = $arr [0];//assumes that the first number is the largest $index = 0;//record the maximum number of key values, i.e. subscript foreach ($arr as $key = > $val) {if ($max < $val) {$max = $val; $index = $key;}} The maximum number is: 887, the subscript is: 8echo ' <br> maximum number is: '. $max. ', its subscript is: '. $index;/* $mval = max ($arr), echo ' <br> ';p rint_r (Array_search ($mval, $arr)); *//** * 4, create an array of length 10, the elements of the array satisfies the law of the Fibonacci Pollazzi sequence; */$arr = array (n); for ($i =2; $i <10; $i + +) {$arr [] = $arr [$i -1]+ $arr [$i-2];} Echo ' <br> ';//array ([0] = 1 [1] = 1 [2] = 2 [3] = 3 [4] = 5 [5] = 8 [6] [7] = = [8] = [9] = Print_r ($arr);/** * 5, calculates the difference between the maximum and minimum numbers in an array; */$arr = array (1,5,67,8,4,3,45,6,887,2);/* System functions simple implementation * $MINV = min ($arr), $MAXV = max ($arr), Echo ' <br> ', echo ' Maximum number is: '. $maxv. ', the minimum number is: '. $minv. ', the difference is: '. ($MAXV-$MINV); *//* does not use system functions to implement */$max = $arr [0];//assumes that the first number is the largest $min = $arr [0];//assumes that the first number is also the smallest foreach ($arr as $key + $val) {if ($max < $val {$max = $val;} ElseIf ($min > $val) {$min = $val;}} Echo ' <br> ';//maximum number is: 887, minimum number is: 1, difference is: 886echo ' Maximum number is: '. $max. ', the minimum number is: '. $min. ', the difference is: '. ($max-$min);/** * 6, write a method, a length of more than 10 of the last 5 items directly intercepted, unchanged order directly into the first 5 items, * such as {1,2,3,4,5,6,7,8,9,10,11,12} after the method has been changed to: { 8,9,10,11,12,1,2,3,4,5,6,7} */function Arrayhandel (& $ary, $count = 5) {if (!) ( Is_array ($ary) && count ($ary) >10 && count ($ary) > $count)) {return false;} $len = count ($ary), while ($count >0) {$i = $len-1; $count--;arRay_unshift ($ary, $ary [$i]); Array_pop ($ary);}} $array = Array (1,2,3,4,5,6,7,8,9,10,11,12,13); Echo ' <br> '; Arrayhandel ($array);//array ([0] = 9 [1] = 10 [2 ] = [3] = [4] = [5] = 1 [6] = 2 [7] = 3 [8] = 4 [9] = 5 [Ten] = 6 [One] = 7 [8] + (Print_r) ($array);/** * * 7, connecting two arrays into a new array; * Array_combine ($keys, $values) a key, a value, but two arrays must be the same number, or return Fals E * Array_merge ($array 1) covers the same key name * Array_merge_recursive ($array 1) does not overwrite the same key name, consisting of a collection of arrays of key names * */$arr 1 = Array (' A ' =>123, ' B ' =>1234); $arr 2 = Array (' OS ' = ' Linux ', ' db ' = ' mysql ', ' language ' = ' php ', ' server ' = ' nginx ', ' b ' = ' = ') ' 1256); Echo ' <br> ';//array ([A] = 123 [b] = 1234 [OS] = Linux [db] + MySQL [language] + PHP [ser Ver] = nginx) print_r ($arr 1+ $arr 2),//$arr 1 will overwrite the same key value in $ARR2, the operation is from right to left, so the left side of the overlay to the right echo ' <br> ';//boolean falsevar_ Dump (Array_combine ($arr 1, $arr 2)),//boolean Falseecho ' <br> ';//array ([a] + 123 [b] = = 1256 [OS] + Linux [db] + MySQL [language] + php [Server] = Nginx) Print_r (Array_merge ($arr 1, $arr 2));//$arr 1 and $arr2 the same element is after a $ ARR2 cover echo ' <br> ';//array ([A] = 123 [b] = = Array ([0] = = 1234 [1] = = 1256) [OS] = Linux [db] = > mysql [language] + php [Server] = Nginx) Print_r (array_merge_recursive ($arr 1, $arr 2))/** * Array in reverse order (cannot use Rsort function directly, cannot generate new array); */$ary = array (' Apple ', ' juice ', ' orange ', ' 0 ', ' app ', ' 1 ', ' 2 ', ' 5 ', ' Z ');/* Rsort ($ary Echo ' <br> ';p rint_r ($ary); */for ($i =0; $i <count ($ary); $i + +) {for ($j = $i +1; $j <count ($ary); $j + +) {if ($ary [$i]< $ary [$j]) {$temp = $ary [$i] ; $ary [$i] = $ary [$j]; $ary [$j] = $temp;}}} Echo ' <br> ';//array ([0] = Z [1] = orange [2] = juice [3] = Apple [4] = + app [5] = 85 [6] =& Gt 5 [7] = 2 [8] = 1 [9] = 0) print_r ($ary);
/** * 9, insert a number in an ascending ordered array of length 10 to form a new array, guaranteeing array order (cannot use sort directly) */$arr = range (1, 2), function insertary ($ary, $val) {$endAry = a Rray (); $len = count ($ary), $index = 0;if ($ary [$len-1] < $val) {//If the number of insertions is greater than any number in the array $ary[] = $val; $endAry = $ary; return $endAr Y }while ($index < $len) {if ($ary [$index]>= $val) {$endAry [$index + +] = $val; for ($i = $index; $i < $len +1; $i + +) {$ endary[$i] = $ary [$i-1]; }break;} else {$endAry [$index] = $ary [$index];} $index + +;} return $endAry;} Echo ' <br> ';//array ([0] = 1 [1] = 3 [2] = 5 [3] = 7 [4] = 9 [5] = [6] = [7] => ; [8] [+] = [9] = [ten] = [one] = [[one]] = [+] = [+] = [+] = [[+]] [31] [16] =& Gt [+]-[+]-[+] [+] [] [] = [+] [] = []] Print_r (Insertary ($arr, 88));
/** * 10, arranges an unordered array into ascending array, (bubble sort) */$arr = array (888,-9,1,11,5,67,8,4,3,45,6,887,2), $len = count ($arr); for ($i =0; $i <$ Len $i + +) {for ($j = $len-1; $j > $i; $j-) {if ($arr [$j]< $arr [$j-1]) {$temp = $arr [$j]; $arr [$j] = $arr [$j-1]; $arr [$j-1] = $ temp;}}} Echo ' <br> ';//array ([0] =-9 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5 [6] = 6 [7] = = 8 [8] = = [9] = [ten] = [one] = 887 [] = 888) print_r ($arr);/********* Select sort ************/$arr = Array (888,-9,1,11,5,67,8,4,3,45,6,887,2), for ($i =0; $i < $len; $i + +) {for ($j = $i +1; $j < $len; $j + +) {if ($arr [$j] < $arr [$i]) {$temp = $arr [$j]; $arr [$j] = $arr [$i]; $arr [$i] = $temp;}}} Echo ' <br> ';//array ([0] =-9 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5 [6] = 6 [7] = = 8 [8] = = [9] = [ten] = [[One] = 887 [] = 888) print_r ($arr);/** * 11, shuffling the ordered array; (Cannot use SHUFFL directly e function) */$arr = range (1, 2), echo ' <br> '; Echo ' <br> $arr upset before: ';//$arr scrambled before: Array ([0] = 1 [1] = 3 [2] = 5 [3] = 7 [4] = 9 [5] = [6] [7] [15] = 8 ] = [9] = [ten] = [one] = 23
[[+] [] [[] [] [] [+] [] [[+] = [+] = [+] = [+] = [+] = [+] = [] [] [] R ($arr), function Shuffledemo ($arr) {$resultAry = Array (), $len = count ($arr), while ($len >0) {$index = rand (0, $len-1); $ resultary[] = $arr [$index];array_splice ($arr, $index, 1); $len--;} return $resultAry;} Echo ' <br> '; Echo ' <br> $arr upset after: ';//$arr upset after: Array ([0] = 3 [1] = [2] = [3] [+] [4] = 5 [5] [+] [6] = 1 [7] + [8] [9] = [ten] = [all] = 27
[[] = [7] = [+] [+] [[+] [] + [+] = 9 [] [+] [] [] [] [] = [+] = +] (s Huffledemo ($arr));
/** * 12, delete the element at the specified position in the array, (refer to unset function) */function deletearyelement (& $arr, $index) {if (! ( Is_array ($arr) && count ($arr) > $index)) {return false;} Unset ($arr [$index]);} $arr = Array (888,-9,1,11,5,67,8,4,3,45,6,887,2);d eletearyelement ($arr, 2); Echo ' <br> ';//array ([0] = 888 [1] +-9 [3] = = [4] = 5 [5] = [6] = 8 [7] = 4 [8] = 3 [9] = [Ten] = 6 [One] = 88 7 [[] = 2) print_r ($arr);
/** * 13, delete the number of primes in the array to form a new array */function Filtfun ($val) {//Custom filter prime function $flag = true;//To determine if the prime if (Is_int ($val)) {if ($val <=0) {$FL AG = FALSE;} for ($i = $val-1; $i >1; $i-) {if ($val% $i ==0) {$flag = False;break;}}} else {$flag = false;} return! $flag;} $arr = Array (888,-9,1,11,5,67,8,4,3,45,6,887,2, ' a '); $rary = Array_filter ($arr, ' filtfun '); Echo ' <br> ';//array ( [0] = = 888 [1] = 9 [6] = 8 [7] = 4 [9] = [Ten] = 6 [] + a) print_r ($rary);
/** * 14, the Write function checks whether an element exists in the array (not directly using In_array) */function Exsitele ($arr, $value) {if (!) ( Is_array ($arr) && count ($arr) >0) {return false;} foreach ($arr as $key = + $val) {if ($val = = $value) {return true;}} return false;} $arr = Array (888,-9,1,11,5,67,8,4,3,45,6,887,2, ' a ', array (' A ' =>14, ' B ' =>48));//boolean Truevar_dump (Exsitele ( $arr, Array (' A ' =>14, ' B ' =>48));/** * 15, write the function, append an element to the array (not directly using Array_push) */$arr = Array (888,- 9,1,11,5,67,8,4,3,45,6,887,2, ' A ', array (' A ' =>14, ' B ' =>48), $arr [] = ' ABCD '; Echo ' <br> ';//array ([0] = = 888 [1] = 9 [2] = 1 [3] + [4] = 5 [5] = [6] = 8 [7] = 4 [8] = 3 [9] = 45 [10] = > 6//[11] = 887 [[A] = 2 [] + a [+] = Array ([a] = [b] = [] =] [[]] [] = ABCD) Print_r ( $arr);/** * 16, write the function, the sum of all elements in the array (not directly using array_sum) */$arr = array (888,-9,1,11,5,67,8,4,3,45,6,-102); function Countary ($ ARR) {if (!) ( Is_array ($arr) && count ($arr) >0) {return false;} $sum = 0;foreach ($arr as $key = $val) {$sum + = $val;} return $sum;} Echo ' <br> ';//927print_r (Countary ($arr));
PHP arrays are very powerful and learn from the practice of PHP arrays. In the future can also deepen the impression, consolidate learning!
PHP Array Exercises 16 questions