PHP array details, array details
As a C ++ programmer, during the PHP development process, some obfuscation occurs on the PHP array, which is similar to the C ++ array, the following gives a comprehensive analysis of PHP arrays and their differences and relationships with the corresponding data types in C ++.
Array category:
1. Value array: it is also called an index array. It uses numbers (starting from 0) as the array subscript. It is equivalent to a vector in C ++.
2. Join array: Use a string as the subscript of the array. It is equivalent to map in C ++.
3. Multi-dimensional array: each element in the array is also an array. Each element in its subarray can also be an array.
Array declaration:
1. Numeric Array
A. In the following example, the digital ID key is automatically assigned. $ Names = array ("Peter", "Joe", "Lily"); B. In the following example, we manually allocate a digital ID key. $ Names [0] = "Peter"; $ names [1] = "Joe"; $ names [2] = "Lily"; these ID keys can be used in scripts: <? Php $ names [0] = "Peter"; $ names [1] = "Joe"; $ names [2] = "Lily"; echo $ names [0]. "and ". $ names [1]. "are ". $ names [2]. "'s neighbors";/* why do you ask hovertree.com */?>
2. Join array:
Example 1 $ ages = array ("Peter" => 32, "Joe" => 30, "Lily" => 28); example 2 this example is the same as Example 1, it is just another way to create an array. $ Ages ["Peter"] = "32"; $ ages ["Joe"] = "30"; $ ages ["Lily"] = "28 "; use the associated array in the script: <? Php $ ages ["Peter"] = "32"; $ ages ["Joe"] = "30"; $ ages ["Lily"] = "28 "; echo "Peter is ". $ ages ["Peter"]. "years old. ";/* why do you ask hovertree.com */?> Output of the above script: Peter is 32 years old.
3. Multi-dimensional array:
In this example, we have created a multidimensional array with the automatically assigned numeric ID key: $ families = array {"Griffin in" => array {"Peter", "Lois ", "Megan"}, "Quagmire" => array {"Glenn"}, "Brown" => array {"Cleveland", "Loretta", "Junior "}}; echo "Is ". $ families ['grigin'] [2]. "a part of the Griffin family? "; Above code output: Is Megan a part of the Griffin family?
Array Traversal
1. for Loop traversal The for loop can only traverse the index array. <? Php $ names = array ("Peter", "Joe", "Lily"); for ($ id = 0; $ id <count ($ names); ++ $ id) {echo $ names [$ id] ;}?> 2. foreach traversal: You can traverse the index array or the associated array to traverse the Index Array foreach (array_expression as $ value) {loop body ;} traverse the associated array foreach (array_expression as $ key = >$ value) {loop body;} A. One-dimensional array traverses the Index Array <? Php $ contact = array ("Lee", "xx Company", "abc@xx.com"); foreach ($ contact as $ value) {echo $ value ;}?> <? Php $ contact = array ("name" => "Lee", "company" => "xx Company", "Mailbox" => "abc@xx.com "); foreach ($ contact as $ key => $ value) {echo $ key. ":". $ value ;}?> B. Multi-dimensional array traversal <? Php $ wage = array ("Marketing Department" = array (1, "Li", "Marketing Manager", 8000), array (2, "Wang ", "Marketing Specialist", 5000), array (3, "Liu", "Marketing Specialist", 7000), "product department" = array (1, "Li", "Product Manager", 9000), array (2, "Wang", "Product specialist", 6000), array (3, "Liu ", "Product specialist", 5000), "Accounting Department" = array (1, "Li", "Account Manager", 7000), array (2, "Wang", "Accounting Specialist", 6000), array (3, "Liu", "Accounting Specialist", 5000 ))); foreach ($ wage as $ section => $ table) {echo $ section. "The team members are as follows"; foreach ($ table as $ row) {forea Ch ($ row as $ value) {echo $ value ;}}/ * What is hovertree.com */?>
Recommended: http://www.cnblogs.com/roucheng/p/phpdongtai.html