Introduction to PHP arrays

Source: Internet
Author: User
Tags add foreach array arrays integer key string variable
Array base:     PHP, the subscript of an array can be an integer, or it can be a string     PHP, the element order of the array is not determined by the subscript, but by the order in which it is "joined", which determines the   definition:     $arr 1 = Array (element 1, Element 2, ...). );     Array (1,1.1,5, ' abc ', TRUE,FALSE); Any data can be stored at this time as the default subscript     Array (2=>1,4=>1.1,3=>5,7=> ' abc ', 0=>TRUE); Subscript can be set arbitrarily (without order, without continuous)     Array (2=>1,1.1,1=>5, ' abc ', 0=>TRUE); You can add subscript, you can also not add, without subscript the default subscript                                        //default subscript rule: The maximum number that has been used before +1                 ;                        //The subscript for this number is: 2,3,1,4,0     A Rray (2=>1, ' dd ' =>5,1=>1.1, ' abc ', 0=>TRUE); Mixed subscript, also follow the default subscript rule     Array ( -2=>1, ' dd ' =>5,1.1, ' abc ', TRUE); Negative numbers are not counted in the integer subscript, but only as characters Poute                                 &NBSp        //the subscript for the last three items of this array is: 0,1,2     Array (2.7=>1, ' dd ' =>5,1=>1.1, ' abc ', TRUE); The RCAs is automatically converted to integers under floating-point numbers and directly removes the decimal part     Array ("2.7" =>1, ' DD ' =>5, "one" =>1.1, ' abc ', TRUE); A pure numeric string subscript, treated as a number     array (2=>1, ' dd ' =>5,true=>1.1, ' abc ', FALSE=>TRUE); Boolean value as Subscript, True is 1,false 0     Array (2=>1, ' dd ' =>5,2=>1.1, ' abc ', TRUE); If the subscript follows the previous repetition, it simply overwrites the target value of the preceding name       Other forms:         $arr 1[] = 1;         $arr 1[] = 5;         $arr 1[] = 1.1;         ...  //directly after the variable use [], becomes an array, and then assigns the         $arr 2[' AA '] = 1;         $arr 2[' bb '] = 5;         $arr 2[5] = 1.1;         ...  //this form of the subscript, in fact, with the array syntax structure almost the same as the category of the   array:     from the key value of the relationship is divided into:         Associative array: usually refers to the subscript as a string, and the string can generally express the meaning of the data of the array            : $person = Array ("name" => "Poe", "Age" =>, "edu" => "University Graduation");         Index array: usually refers to an array of subscript strict starting from 0 consecutive digital subscript--similar to the JS array of     from the array level:         One-dimensional array: is an array of each element value, is a normal value (not an array of values)              : $person = Array ("name" => "Poe", "a GE "=>", "edu" => "graduated from university");         Two-dimensional array: Each item in an array is a one-dimensional array.             $person = Array (                            "name" => Array ("Xiaohua", "Xiaofang),            &NBSP ;                 "age" => Array (18,22),                              "edu" => Array ("University graduate", "elementary School",)       &NBSP ;                             Multidimensional array: by analogy ...。                 General syntax form of multidimensional array:               &NBS P   $V 1 = array name [subscript] [subscript] [...]   array traversal:     TRAVERSAL basic syntax:         foreach ($arr as [$key =&G t;] $value) {           //all possible operations on the $key and $value are available here-because they are a variable       &N Bsp    //$key represents the subscript for each element, possibly a number, or it can be a string            //$value represents each time the value of the element is obtained, possibly various types            //This loop structure is traversed from the first item of the array to the last item, then the         {}   array pointer and traversal principle: &N Bsp   Each array has a "pointer" inside it that determines which element     foreach traversal is being taken when the array is currently fetched, depending on the pointer.     Example: $arr 1 = Array (2=>1, ' dd ' =>5,1=>1.1, ' abc ', 0=>TRUE);     Wkiol1znx1sxflt6aaanrltn90q498.jpg Pointers in addition to the position setting for the Foreach loop, some other functions also depend on pointers: 1: $v 1 = current ($arr 1);    //Gets the value of the element that the current pointer points to in $ARR1, or False 2: $v 1 = key ($arr 1) If there is no point to the element;        //Gets the subscript of the element that the current pointer points to in the $arr1 ... 3: $v 1 = Next ($arr 1);      //move the pointer to the next element and get the value of the next element 4: $v 1 = prev ($arr 1);      //move the pointer to the previous element and get the value of the previous element 5: $v 1 = reset ($arr 1);      //Moves the pointer to the first element and obtains the value of the element 6: $v 1 = end ($arr 1);        //moves the pointer to the last element and gets the value of the element 7: $v 1 = each ($arr 1);      //get subscript and value for the current element, then move the pointer to the next position  

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.