Home >Backend Development >PHP Tutorial >Example analysis of PHP arrays
This article mainly introduces the relevant information on the detailed explanation of PHP array examples. Arrays are divided into array numerical values, associative arrays, and multi-dimensional arrays. The introduction in this article is very good and has reference value. Friends who need it can refer to it
As a C++ programmer, in the process of switching to PHP development, I had some confusion about PHP arrays. They are similar to C++ arrays, but also have some differences. Let’s comprehensively analyze PHP arrays and their differences. The differences and connections between corresponding data types in C++.
Classification of arrays:
1. Numeric array: also called index array, that is, using numbers (starting from 0) as array subscripts . Equivalent to vector in C++.
2. Associative array: Use string as array subscript. Equivalent to map in C++.
3. Multidimensional 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, numeric ID keys will be automatically assigned .
$names = array("Peter","Joe","Lily");
b. In the following example, we manually assign numeric ID keys.
$names[0] = "Peter";
$names[1] = "Joe";
$names[2] = "Lily";
You can use these ID keys in scripts:
##
<?php $names[0] = "Peter"; $names[1] = "Joe"; $names[2] = "Lily"; echo $names[0]." and ".$names[1]." are ".$names[2]."'s neighbors"; /* 何问起 hovertree.com */ ?>2. Associative array: Example 1$ages = array( "Peter"=>32, "Joe"=>30, "Lily"=>28);Example 2This example is the same as Example 1, just another Method to create an array.
$ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28";Using associative arrays in scripts:
$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 iterate over 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 associative arrayTraverse Index arrayforeach(array_expression as $value) { 循环体; } 遍历关联数组 foreach(array_expression as $key=>$value) { 循环体; }A. One-dimensional array traversalIndex array
<?php $contact = array("李某","xx公司","abc@xx.com"); foreach($contact as $value) { echo $value; } ?> <?php $contact = array("姓名"=>"李某","公司"=>"xx公司","邮箱"=>"abc@xx.com"); foreach($contact as $key=>$value) { echo $key.":".$value; } ?>B. Multidimensional array traversal
<?php $wage = array( "市场部"=array( array(1,"李某","市场经理",8000), array(2,"王某","市场专员",5000), array(3,"刘某","市场专员",7000) ), "产品部"=array( array(1,"李某","产品经理",9000), array(2,"王某","产品专员",6000), array(3,"刘某","产品专员",5000) ), "账务部"=array( array(1,"李某","账务经理",7000), array(2,"王某","账务专员",6000), array(3,"刘某","账务专员",5000) ) ); foreach($wage as $section=>$table) { echo $section."部门人员如下"; foreach($table as $row) { foreach($row as $value) { echo $value; } } } /* 何问起 hovertree.com */ ?>
The above is the detailed content of Example analysis of PHP arrays. For more information, please follow other related articles on the PHP Chinese website!