As a C + + programmer, in the process of PHP development, the PHP array generated some confusion, and C + + arrays have similar places, there are some differences, the following comprehensive analysis of the PHP array and its corresponding data types in C + + and the relationship between.
Classification of arrays:
1, array of values: Also known as the index array, that is, the number (starting from 0) as an array subscript. Equivalent to a vector in C + +.
2. Associative array: Subscript the array as a string. Corresponds to a map in C + +.
3. Multidimensional arrays: Each element in an array is also an array. Each element in its sub-array can also be an array.
Declaration of the array:
1. Array of values
A, in the following example, the numeric ID key is assigned automatically. $names=Array("Peter", "Joe", "Lily"); b, in the example below, we manually assign the numeric ID key. $names[0] = "Peter"; $names[1] = "Joe"; $names[2] = "Lily"; You can use these ID keys in your script:<?PHP$names[0] = "Peter"; $names[1] = "Joe"; $names[2] = "Lily"; Echo $names[0]. " and ".$names[1]. " Is ".$names[2]. "' S Neighbors "; /*how to ask Hovertree.com*/?>
2. Associative arrays:
Example 1$ages=Array("Peter" =>32, "Joe" =>30, "Lily" =>28); Example 2 This example is the same as Example 1, just another way to create an array. $ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28"; To use associative arrays in a script:<?PHP$ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28"; Echo"Peter is".$ages["Peter"]. " Years old. "; /*how to ask Hovertree.com*/?>above script output: Peter isYears old.
3. Multidimensional Arrays:
In this example, we created a multidimensional array with an auto-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?"; Above code output: is Megan a part of the Griffin family?
Array traversal
1, the For loop traversal for loop can only traverse an indexed array. <?PHP$names=Array("Peter", "Joe", "Lily"); for($id= 0;$id<Count($names);++$id) { Echo $names[$id]; } ?> 2, a foreach traversal can traverse an indexed array, or it can traverse an array of indexesforeach(array_expression as $value) {loop body; } Traversal associative arrayforeach(array_expression as $key=$value) {loop body; } A, one-dimensional array traversal index array<?PHP$contact=Array("Li MoU", "xx Company", "[email protected]"); foreach($contact as $value) { Echo $value; } ?> <?PHP$contact=Array("Name" = "Li MoU", "Company" = "xx Company", "e-mail" = "[email protected]"); foreach($contact as $key=$value) { Echo $key.":".$value; } ?>B, multidimensional array traversal<?PHP$wage=Array( "Marketing Department" =Array( Array(1, "Li MoU", "Marketing Manager", 8000),Array(2, "Wang MoU", "Market commissioner", 5000),Array(3, "Liu MoU", "Market commissioner", 7000) ), "product Department" =Array( Array(1, "Lee", "Product Manager", 9000),Array(2, "Wang MoU", "Product Specialist", 6000),Array(3, "Liu MoU", "Product Specialist", 5000) ), "accounts Department" =Array( Array(1, "Lee", "Account Manager", 7000),Array(2, "Wang MoU", "Account Officer", 6000),Array(3, "Liu MoU", "Account Officer", 5000) ) ); foreach($wage as $section=$table) { Echo $section." Department personnel as follows "; foreach($table as $row) { foreach($row as $value) { Echo $value; } } } /*how to ask Hovertree.com*/?>
Recommendation: http://www.cnblogs.com/roucheng/p/phpdongtai.html
An explanation of the PHP array