PHP array instance details, php 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 ";
You can use these ID keys in the script:
<? 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 method for creating arrays.
$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 */?>
The above Script output:
Peter is 32 years old.
3. Multi-dimensional array:
In this example, we create a multi-dimensional array with the automatically assigned numeric ID key:
$families = array { "Griffin"=>array { "Peter", "Lois", "Megan" }, "Quagmire"=>array { "Glenn" }, "Brown"=>array { "Cleveland", "Loretta", "Junior" } }; echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
The above code output:
Is Megan a part of the Griffin family?
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.
Traverse the Index Array
Foreach (array_expression as $ value) {loop body;} traverses the correlated array foreach (array_expression as $ key =>$ value) {loop body ;}
A. One-dimensional array Traversal
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 */?>
The above is a detailed description of the php array instance provided by the small Editor, hoping to help you.