PHP Array Introduction _php Tutorial

Source: Internet
Author: User

Introduction to PHP arrays


Array Basics: In PHP, the subscript of an array can be an integer or a string in PHP, and the order of elements in an array is not determined by the subscript, but is defined by the order in which it "joins": $arr 1 = Array (element 1, Element 2, ...). ); Array (1,1.1,5, ' abc ', TRUE,FALSE); can store any data at this time as the default subscript array (2=>1,4=>1.1,3=>5,7=> ' abc ', 0=>TRUE); Subscript can be arbitrarily set (no order, no continuous) array (2=>1,1.1,1=>5, ' abc ', 0=>TRUE); You can add subscript, or do not add the subscript is the default subscript//default subscript rule: The largest number of previous used subscript +1//The number subscript is: 2,3,1,4,0 Array (2=>1, ' dd ' =>5,1=>1.1, ' abc ' , 0=>true); Mix subscript, also follow the default subscript rule array ( -2=>1, ' dd ' =>5,1.1, ' abc ', TRUE); The negative subscript is not counted in the integer subscript, but only as the word Poute//Then the subscript of the last three items of this array is: 0,1,2 Array (2.7=>1, ' dd ' =>5,1=>1.1, ' abc ', TRUE); Floating-point bidding clubs are automatically converted to integers, and the fractional part of the array ("2.7" =>1, ' dd ' =>5, ' one ' =>1.1, ' abc ', true) is removed directly; 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 for 1,false is 0 array (2=>1, ' dd ' =>5,2=>1.1, ' abc ', TRUE); If the subscript is repeated with the preceding, then the other form of the underlying value with the same name is covered simply: $arr 1[] = 1; $arr 1[] = 5; $arr 1[] = 1.1; ...//directly after the variable is used [], it becomes an array, and in turn assigns a value $arr 2[' AA '] = 1; $arr 2[' bb '] = 5; $arr 2[5] = 1.1; ...//This form of subscript, in fact, with the use of the array syntax structure almost the same as the classification of arrays: from the key value relationship is divided into: Associative arrays: an example of an array that typically refers to the subscript as a string, and that string can roughly express the meaning of the data: $person = Array ("name" = "Poe", "Age" = "edu" = "University Graduate"); Indexed array: usually refers to an array of subscript strict 0-based continuous digital subscript-like the JS array from the array hierarchy: a one-dimensional array: is an array of each element value, is a normal value (non-array value) Example: $person = Array ("name" and "=" Poe "," Age "," edu "=" University Graduate "); Two-dimensional arrays: Each item in an array is another one-dimensional array. $person = Array ("name" = = Array ("Xiaohua", "Xiaofang"), "age" = = Array (18,22), "edu" = = Array ("University graduate", "primary School",)); Multidimensional arrays: In turn ... The general syntactic form of multidimensional arrays: $v 1 = array name [subscript] [subscript] [...] array traversal: Traversal basic Syntax: foreach ($arr as [$key =>] $value) {//$key and $value can be all possible Operations--because they are a variable//$key represents the subscript for each element that is obtained, either a number or a string//$value represents the value of each acquisition element, possibly various types//this loop structure will traverse from the first item of the array to the last item and then end} Array pointers and traversal principles: each array has a "pointer" inside it, which determines the element that is taken when the array is currently taking the value, which is dependent on the pointer during the foreach traversal. Example: $arr 1 = Array (2=>1, ' dd ' =>5,1=>1.1, ' abc ', 0=>TRUE); The wkiol1znx1sxflt6aaanrltn90q498.jpg pointer is in addition to the position setting for the Foreach loop, and some other functions depend on the pointer: 1: $v 1 = current ($arr 1); Gets the value of the element that the current pointer points to in $ARR1, or false2 If no element is pointed to: $v 1 = key ($arr 1); 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); Move the pointer to "first element" and get the value of the element 6: $v 1 = end ($arr 1); Move the pointer to the last element and get the value of the element 7: $v 1 = each ($arr 1); Gets the subscript and value of the current element, and then moves the pointer to the next position

http://www.bkjia.com/PHPjc/1073352.html www.bkjia.com true http://www.bkjia.com/PHPjc/1073352.html techarticle PHP Arrays Introduce the basics of arrays: in PHP, the subscript of an array can be an integer, or it can be a string in PHP, and the order of elements in an array is not determined by the subscript, but by the order in which it is added ...

  • 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.