Summary of common php array Function instances [assignment, splitting, merging, calculation, addition, deletion, query, Judgment, sorting], array
This example summarizes the commonly used array functions of php. We will share this with you for your reference. The details are as follows:
Array_combine
Function: Use the value of an array as the key name of the new array, and the value of another array as the value of the new array.
Case:
<? Php $ a = array ("one", "two", "three"); $ B = array ("1", "2", "3 "); $ c = array_combine ($ a, $ B); print_r ($ c ); /** result * Array ([one] => 1 [two] => 2 [three] => 3 )*/
Array_chunk
Function: Split scores to form multiple arrays.
<? Php $ input_array = array ("a" => "apple", "B" => "blue", "c", "d", "e "); echo "<pre>"; print_r (array_chunk ($ input_array, 2); print_r (array_chunk ($ input_array, 2, True); echo "</pre> "; /** result Array ([0] => Array ([0] => apple [1] => blue) [1] => Array ([0] => c [1] => d) [2] => Array ([0] => e )) array ([0] => Array ([a] => apple [B] => blue) [1] => Array ([0] => c [1] => d) [2] => Array ([2] => e ))*/
Array_count_values
Function: counts the number of times a group's values appear.
<? Php $ input_array = array ("a" => "apple", "B" => "blue", "c", "d", "e "); echo "<pre>"; print_r (array_count_values ($ input_array); echo "</pre> "; /** result Array ([apple] => 1 [blue] => 1 [c] => 1 [d] => 1 [e] => 1 )*/
Array_diff
Function: remove the data in the second array from the first array and return the remaining content as the result.
<? Php $ array1 = array ("a" => "apple", "B" => "blue", "c", "d", "e "); $ array2 = array ("apple", "c", "d", "f"); $ result = array_diff ($ array1, $ array2 ); $ result2 = array_diff ($ array2, $ array1); echo "<pre>"; print_r ($ result ); // remove the remaining print_r ($ result2) in array 2 from array 1; // remove the remaining echo in array 1 from array 2 "</pre> "; /** result Array ([B] => blue [2] => e) Array ([3] => f )*/
Array_map
Function: run the callback function in the array.
<? Php // define the callback function cube ($ n) {return ($ n * $ n);} $ a = array (1, 2, 3, 4, 5 ); $ B = array_map ("cube", $ a); echo "<pre>"; print_r ($ B); echo "</pre> "; /** result Array ([0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )*/
Array_merge
Function: Merge one or more arrays.
Note: If the key name is the same, the previous content will be overwritten, and the key name and number will be added to the back
<? Php $ array1 = array ("color" => "red", 2, 4); $ array2 = array ("a", "B", "color" => "green ", "shape" => "trapezoid", 4); $ result1 = array_merge ($ array1, $ array2); $ result2 = array_merge_recursive ($ array1, $ array2 ); echo "<pre>"; print_r ($ result1); print_r ($ result2); echo "</pre> "; /** result Array ([color] => green [0] => 2 [1] => 4 [2] => a [3] => B [shape] => trapezoid [4] => 4) array ([color] => Array ([0] => red [1] => green) [0] => 2 [1] => 4 [2] => a [3] => B [shape] => trapezoid [4] => 4 )*/
Array_pop
Function: removes the last element of the array and returns the excluded element content.
<? Php $ stack = array ("orange", "banana", "apple", "1"); $ last = array_pop ($ stack); echo "<pre> "; print_r ($ stack); print_r ($ last); echo "</pre> "; /** result Array ([0] => orange [1] => banana [2] => apple) 1 */
Array_push
Function: Press multiple units to the end of the array and return the number of arrays.
<? Php $ stack = array ("orange", "banana"); $ count = array_push ($ stack, "apple", "red", "blue "); echo "<pre>"; print_r ($ stack); print_r ($ count); echo "</pre> "; /** result Array ([0] => orange [1] => banana [2] => apple [3] => red [4] => blue) 5 */
Array_rand
Function: Obtain random key names.
<? Php $ input = array ("orange", "banana", "apple", "red", "blue"); $ rand = array_rand ($ input, 2 );; print_r ($ rand); $ rand = array_rand ($ input, 3); print_r ($ rand ); /** result Array ([0] => 1 [1] => 4) Array ([0] => 0 [1] => 1 [2] => 3) */
Array_search
Function: queries the content in the array and returns the key value. If multiple matches exist, the first match is returned.
<? Php $ array = array ("blue" => "B", "red" => "r", "green", "r "); $ key = array_search ('B', $ array); echo $ key; echo "<br>"; $ key = array_search ('R', $ array ); echo $ key; echo "<br>";/** result bluered */
Array_shift
Function: Removes elements starting with array_pop.
<? Php $ fruit = array ("milk", "orange", "banana", "apple"); $ top = array_shift ($ fruit); print_r ($ top ); echo "<br>"; print_r ($ fruit);/** result: milkArray ([0] => orange [1] => banana [2] => apple )*/
Array_unique
Function: removes repeated elements from the array and retains the first element that appears, including the key name and value.
<? Php $ input = array ("a" => "green", "red", "B" => "green", "blue ", "c" => "red"); $ result = array_unique ($ input); print_r ($ result); echo "<br>"; print_r ($ input ); /** result Array ([a] => green [0] => red [1] => blue) array ([a] => green [0] => red [B] => green [1] => blue [c] => red )*/
Array_slice
Function: Extracts some elements from the array.
<? Php $ input = array ("a", "B", "c", "d", "e"); $ output = array_slice ($ input, 2 ); // The second parameter is optional, indicating that the last element print_r ($ output); echo "<br>"; $ output = array_slice ($ input,-) is obtained ); // when the second parameter is a positive number, it indicates the number. The last parameter is-1, and the last parameter is-2print_r ($ output). echo "<br> "; $ output = array_slice ($ input, 0, 3); print_r ($ output); echo "<br>"; $ output = array_slice ($ input, 2,-1 ); // when the second parameter is a negative number, it indicates the position, which one is obtained, excluding itself print_r ($ output); echo "<br> "; $ output = array_slice ($ input, 2,-1, true); // if the third parameter is true, retain the original key value print_r ($ output ); echo "<br>";/** result Array ([0] => c [1] => d [2] => e) array ([0] => d) Array ([0] => a [1] => B [2] => c) array ([0] => c [1] => d) Array ([2] => c [3] => d )*/
Count
Function: returns the number of elements in an array.
<? Php $ input = array ("a", "B", "c", array ("d", "e"); $ count = count ($ input ); echo $ count; echo "<br>"; $ input = array ("a", "B", "c", "d", "e "); $ count = count ($ input); echo $ count;/** result 45 */
Current
Function: gets the current pointer to an element.
<? Php $ array = array ("foot", "bike", "car", "plane"); $ result = current ($ array); echo $ result. "<br>"; next ($ array); // point the pointer to the next element $ result = current ($ array); echo $ result. "<br>"; prev ($ array); // make the pointer to the forward element $ result = current ($ array); echo $ result. "<br>"; end ($ array); // point the pointer to the last element $ result = current ($ array); echo $ result. "<br>";/** result: footbikefootplane */
In_array
Function: checks whether a value exists in an array. If yes, True is returned. If no value exists, False is returned.
<? Php $ OS _list = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $ OS _list )) {echo "Irix exists in the current operating system list";} else {echo "Irix does not exist in the current operating system list";} echo "<br> "; if (in_array ("mac", $ OS _list) {echo "mac exists in the current operating system list";} else {echo "mac does not exist in the current operating system list ";} echo "<br>";/** result: the current operating system list contains Irix. The current operating system list does not contain mac */
List
Function: assigns information in the array to multiple variables.
<? Php $ info = array ("red", "blue", "green"); list ($ flag, $ sky, $ grassland) = $ info; echo "$ flag, $ sky, $ grassland "; echo" <br> "; list ($ flag, $ grassland) = $ info; echo" $ flag, $ grassland "; echo "<br>"; list (, $ grassland) = $ info; echo "$ grassland"; echo "<br>";/** result red, blue, greenred, greengreen */
Shuffle
Function: disrupt the array.
<? Php $ numbers = range (1, 5); // generate a random array print_r ($ numbers); echo "<br/>"; shuffle ($ numbers ); // disrupt the array print_r ($ numbers ); /** result Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) array ([0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 )*/
Array_keys
Function: gets the key name of an array. The second parameter can be used to obtain an element.
<? Php $ array = array (0 => 100, "color" => "red"); print_r (array_keys ($ array); echo "<br> "; $ array = array ("blue", "red", "green", "blue", "blue"); print_r (array_keys ($ array, "blue ")); echo "<br>"; $ array = array ("color" => array ("blue", "red", "green "), "size" => array ("small", "medium", "large"); print_r (array_keys ($ array); echo "<br> "; /** result Array ([0] => 0 [1] => color) Array ([0] => 0 [1] => 3 [2] => 4) array ([0] => color [1] => size )*/
Array_reverse
Function: returns the reverse direction of an array.
<? Php $ input = array ("php", 3.0, array ("green", "red"); $ result = array_reverse ($ input ); // disrupt key name $ result_keyed = array_reverse ($ input, TRUE); // retain key name print_r ($ result); print_r ($ result_keyed ); /** result Array ([0] => Array ([0] => green [1] => red) [1] => 3 [2] => php) array ([2] => Array ([0] => green [1] => red) [1] => 3 [0] => php )*/
Arsort
Function: reverse sorting without changing indexes
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); arsort ($ fruits); // sort by character in reverse order or number foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br>" ;}/ ** Result B = orangea = lemonc = bananad = apple */
Asort
Function: Performs positive sorting.
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); arsort ($ fruits); // sort by character in reverse order or number foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br>" ;}echo "<p>"; asort ($ fruits ); // forward sort by character or number foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br> ";} /** Result B = orangea = lemonc = bananad = appled = applec = bananaa = lemonb = orange */
Krsort
Function: reverse sorting by key name
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); krsort ($ fruits); // sort by key name or number foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br>" ;}/ ** result d = applec = bananab = orangea = lemon */
Ksort
Function: Performs forward sorting by key name.
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); ksort ($ fruits); // sort by key name or number foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br>" ;}/ ** result a = lemonb = orangec = bananad = apple */
Rsort
Function: Performs reverse sorting by value and changes the key name.
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); rsort ($ fruits); // sort by value in reverse order or number, key name change foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br> ";} /** result 0 = orange1 = lemon2 = banana3 = apple */
Sort
Function: Performs Positive Sorting by value and changes the key name.
<? Php $ fruits = array ("a" => "lemon", "B" => "orange", "c" => "banana ", "d" => "apple",); sort ($ fruits); // sort by value in reverse order or number, key name change foreach ($ fruits as $ key => $ val) {echo "$ key = $ val <br> ";} /** result 0 = apple1 = banana2 = lemon3 = orange */