PHP Array Basics

Source: Internet
Author: User


Declaration of PHP Arrays     &NBSP;1, arrays can have any type of data      2, length can be extended      3, array of categories:          A, indexed arrays: arrays are indexed in 0-based frames                 declaration: $arr = array; $a Rr[0] = 1;                Calculate the length of the array: Count ($arr);          B, off Inline arrays: Arrays are indexed as strings                : $arr ["name"] = "gh";                      $arr = Array ("name" = "Zs", "Age" =>2);       &NBS P   Integration of indexed arrays and associative arrays using:          $arr = Array ("name" = "Zs", 2, "age" =>3,4);    & nbsp     If you want to go to name is $arr["name"], if you want to get to 2, that is $arr[0], if you want to get 4, that is arr[1], the subscript of the associative array does not affect the sorting of the indexed array            Multidimensional arrays:                $arr 1 = array (1,2,3,4);    &nbs P           $aRR2 = Array ("abc", "Def");                 $arr = Array ($arr 1, $arr 2);  &NB Sp                       Second, PHP array traversal      1, for loop: fo R ($i =0;$<count ($arr); $i + +) {} Try not to use it, because indexed arrays and associative arrays may be mixed using      2, foreach ($arr as $value) {} or foreach ($ Arr as $key = $value) {} where $key represents the subscript for the array, $value represents the value of the array        traversing the two-dimensional array:      $arr = Array ( Array ("A", "B");    foreach ($arr as $key = = $value) {
foreach ($value as $k = = $v) {
echo "\ $arr [". $key. "] [". $k."] = ". $v." &nbsp;&nbsp; ";
}
echo "<br/> traverse a line break <br/>";
} 3, while () $arr = Array ("Hello" =>1,2,3); Each () Method: This method returns an array, such as array ([1] = 1 [value] = 1 [0] = = Hello [key] = Hello), 0 and key represent the following table of the array, and 1 and value represent the values corresponding to the following table.           Perform a step forward after each execution until the data is not in the array print_r (each ($arr)); while ($a = each ($arr)) {echo $a ["key"]. "       ====> ". $a [" Value "]} 4, List () converts the values in the array to variables, only the indexed array $arr = array (All-in-a-box);     List ($a, $b, $c) = $arr; echo $a. " -----". $b."      -----". $c; while (list ($key, $value) =each ($arr)) {}
Third, the PHP array of related processing functionscurrent (array arr): Returns the value of the content that corresponds to the present pointerkey (Array arr): Returns the value of the index corresponding to the current pointerNeither of these functions can move the pointer $arr = array ("Hello" = "one", "two", "three", and "one"); $a = current ($arr), returns the value corresponding to the present pointer, outputs one $b = key ($arr), returns the value of the index corresponding to the current pointer position, outputs the HelloNext (Array arr): Moves the pointer of the array down oneEnd (Array arr): Moves the pointer of the array to the lastprev (array arr): Moves the pointer of the array up oneReset (Array arr): Moves the pointer of the array back to the first position count (Array arr): Calculates the length of an arraysizeof (array arr): Calculate array length array_change_key_case (array arr, morphology constant): Changshu form: Case_upper,case_lower     $arr = Array ("heLLo" = "one", "WWW" = "n", "Baidu" = "three");     $newarr = Array_change_key_case ($arr, Case_upper);          Print_r ($newarr); The result of the output is: Array ([HELLO] + one [WWW] = [BAIDU] = three)array_chunk (target array, number of indexes [, Boolean]): decomposes an array into an array of multiple small data, and the last Boolean, if true, indicates that the index name of the previous array is preserved, otherwise it is all in the form of an indexed array. This value defaults to False Example:$arr = Array ("Hello" = "one", "one", "three", "one", "www");     $newarr = Array_chunk ($arr, 3,true); The output is: Array ([0] = = Array ([Hello] = + one [0] = [1] = three) [1] = = Array ([2] = 1 [3] +/-[+] = +] 2 [4] = 3) [2] = = Array ([5] = www)) When the last parameter is false: Array ([0] = = Array ([0] = one [1] =& Gt Both [2] = three) [1] = = Array ([0] = 1 [1] = 2 [2] = 3) [2] = = Array ([0] = www)) can be seen All indexes in the second mode are numbersarray_count_values (array name): Used to calculate the number of occurrences of a value in an array, the returned value is still the array, the value in the original array as the current array of subscripts, the number of times as the length of the arraysuch as:$arr = Array ("Hello" = "one", "one", "three", "one", "one", "one", and "www");     $newarr = Array_count_values ($arr);     Print_r ($newarr); The result of the output is: Array ([one] = 2 [One] = 2 [three] = 1 [1] = 2 [2] = 2 [3] = 2 [www] + 1)Array_fill (start index position, segment size, specified character): Fills the segment of the specified index in the array with a custom value array_filter (target array, defined function): Filter functionExample:     $arr = Array ("Hello" = "one", "one", "three", 1,2,3,-5,-7, "www");          function Func1 ($value) {if ($value > 0) {return true;     }else return false; } $newarr = Array_filter ($arr, func1);//indicates that each value in the ARR array is passed as a parameter to the func1 for comparison, and if true is persisted to the newarr array, otherwise discarded (this place, All strings are recognized as 0).array_walk (array arr,function func1[,userdata]):Applies a callback function to each element in the array.     Returns TRUE if successful, otherwise FALSE.     function Func1 ($value, $key) {echo "$key--$value <br/>";     } $arr = Array ("a" = "cat", "b" = "dog");     Array_walk ($arr, func1); The output is: $a--cat $b--and a slightly more complex example of the dog, which you can refer to here: Http://www.w3school.com.cn/php/func_array_walk . aspArray_map (callback function, array arr): Functions The callback function on the cell of the given arrayExample: function Func1 ($value) {return $value * $value;          } $newarr = Array_map (func1, $arr); At this point, $newarr is an array, which holds each worth of squared values in the $arr array_flip (target array): Swaps the keys and values in the array, and if the same value occurs more than once, the last key will be the value, and all the others are lost.  array_sum (destination array): Returns the value of all the values in the array and the result of an integer or floating-point number. Array_unique (): Removes duplicate values from the array          array_values (): Returns all values in the array, ignores the original key name, and re-uses the indexed array     Array_keys (Array arr): Returns all the keys in the array      In_array (the element to retrieve, array Arr[,boolean is retrieved by data type]): Finds whether a value is in an array     Array_search (element to find, array $arr): Returns the corresponding element corresponding to the key name, there is no return false array_key_exists (subscript value, array $arr): Determines whether the subscript exists, returns a Boolean     Extract (Array $arr): Converts an array to a variable, only for associative arrays Compact (): Converts a variable into an array, resulting in an associative array with the same key value and content as the array 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.