This article mainly introduces the array processing functions in PHP, and summarizes and analyzes common PHP array processing functions, definitions, and usage techniques in combination with examples. the code is provided with detailed annotations for ease of understanding, for more information, see the examples in this article to summarize the array processing functions in PHP. We will share this with you for your reference. The details are as follows:
<? Php // change the case sensitivity of the array key $ arr1 = array ("a" => "Lamp", "db" => "database ", "LANGUAGE" => "PHP"); print_r (array_change_key_case ($ arr1, CASE_UPPER); echo"
"; Print_r (array_change_key_case ($ arr1, CASE_LOWER); echo"
"; // Divide an array into multiple third parameters to set whether to retain the key $ arr2 = array ('A', 'B', 'C', 'D ', 'E', 'F', 'g'); print_r (array_chunk ($ arr2, 2, true); echo"
"; Print_r (array_chunk ($ arr2, 2, false); echo"
"; // Array array_diff_assoc (array $ array1, array $ array2 [, array $...]) returns an array, // This array includes all the values in array1 but not in any other parameter array. // for different keys, $ arr3 = array ('a' => 'green ', 'B' => 'brown ', 'C' => 'red'); $ arr4 = array ('a' => 'green', 'yellow ', 'Red'); print_r (array_diff_assoc ($ arr3, $ arr4); echo"
"; // Array_diff (array $ array1, array $ array2 [, array $...]) // returns an array containing all values in the array of array1 but not in any // Other parameter arrays. if the key is different, it is not considered print_r (array_diff ($ arr3, $ arr4 )); echo"
"; // Array_fill (int $ start_index, int $ num, mixed $ value) // fill an array with num entries with the value of the value parameter, // The key name is specified by the start_index parameter. Print_r (array_fill (-5, 8, "banana"); echo"
"; // Array_flip (array $ trans) // returns an inverted array. for example, the key name in trans is changed to a value, and the value in trans is changed to a key name. $ Arr5 = array ('a' => '1', "B" => "2", "c", "d", "e "); print_r (array_flip ($ arr5); echo"
"; // Array_map (callback $ callback, array $ arr1 [, array $...]) // returns an array that contains all the elements in arr1 that have passed the call back. The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () // function. Function cube ($ n) {return $ n * $ n;} $ arr6 = array (1, 2, 4, 5); print_r (array_map ("cube", $ arr6 )); echo"
"; // Array_merge_recursive (array $ array1 [, array $...]) // combine the elements of one or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results. If the input array has the same string key name, // These values will be merged into an array, which will be recursive, therefore, if a value itself // is an array, this function combines it into another array according to the corresponding entries. However, for example, if the // Array has the same array key name, the latter value will not overwrite the original value, but will be appended to. $ Arr7 = array ("color" => array ("favorite" => "red"), 5); $ arr8 = array (10, array ("favorite" => "yellow", "blue"); print_r (array_merge_recursive ($ arr7, $ arr8); echo"
"; // Array_reduce (array $ input, callback $ function [, int $ initial] // iteratively applies the callback function to every unit in the input array, to simplify the array // to a single value. If the optional parameter initial is specified, the parameter is treated as the // value in the array, or the final return value if the array is empty. If the array is empty and no initial parameter is passed, array_reduce () returns NULL. Function rsum ($ v, $ w) {$ v + = $ w; return $ v;} function rmul ($ v, $ w) {$ v * = $ w; return $ v ;}$ a = array (1, 2, 3, 4, 5); $ x = array (); $ B = array_reduce ($ a, "rsum "); $ c = array_reduce ($ a, "rmul", 10); $ d = array_reduce ($ x, "rsum", 1); echo $ B. "\ t ". $ c. "\ t ". $ d. "\ n"; echo"
"; // Array_replace (array & $ array, array & $ array1 [, array & $ array2 [, array & $...]) // The function replaces the value of the first array with the value of the following array element. If a key exists in the first/array and the second array, its value will be replaced by the value in the second array. If a // key exists in the second array but does not exist in the first array, this // element is created in the first array. If a key only exists in the first array, it remains unchanged. If multiple replicas are passed, they are processed in order, and the subsequent arrays overwrite the previous values. $ Base = array ("orange", "banana", "apple", "raspberry"); $ replacements = array (0 => "pineapple ", 4 => "cherry"); $ replacements2 = array (0 => "grape"); // print_r (array_replace ($ base, $ replacements, $ replacements2 )); // echo"
"; // Array_splice (array & $ input, int $ offset [, int $ length [, array $ replacement]) // remove the units specified by offset and length from the input array. if the replacement // parameter is provided, replacement is used. Returns an array containing the removed cells //. Note that the number key name in input is not retained. If length is omitted, all parts from // offset to the end of the array are removed. If length is specified and it is a positive value, remove so many units //. If length is specified and it is a negative value, all units in the center from offset to the end of the array are removed. Tips: when replacement is given, count ($ input) is used as the length to remove all units from the offset to the end of the // array. $ Input = array ("red", "green", "blue", "yellow"); array_splice ($ input, 1,-1); print_r ($ input ); echo"
"; // Key (array & $ array) // return the key name of the current cell in the array. $ Fruit = array ("fruit1" => "apple", "fruit2" => "orange", "fruit3" => "grape ", "fruit4" => "apple", "fruit5" => "apple"); while ($ fruit_name = current ($ fruit )) {if ($ fruit_name = 'apple') {echo key ($ fruit )."
";}Next ($ fruit);} echo"
";?>
I hope this article will help you with PHP programming.