Home  >  Article  >  Backend Development  >  Detailed explanation of PHP array examples_php examples

Detailed explanation of PHP array examples_php examples

WBOY
WBOYOriginal
2016-07-06 13:32:12873browse

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 and connections with corresponding data types in C.

Classification of arrays:

1. Numeric array: also called index array, that is, numbers (starting from 0) are used 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.

Declaration of array:

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";

These ID keys can be used in scripts:

<&#63;php 
$names[0] = "Peter"; 
$names[1] = "Joe"; 
$names[2] = "Lily"; 
echo $names[0]." and ".$names[1]." are ".$names[2]."'s neighbors"; 
/* 何问起 hovertree.com */
&#63;> 

2. Associative array:

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"; 

Using associative arrays in scripts:

<?php 
$ages["Peter"] = "32"; 
$ages["Joe"] = "30"; 
$ages["Lily"] = "28"; 
echo "Peter is ".$ages["Peter"]." years old."; 
/* 何问起 hovertree.com */
?> 

The above script output:

Peter is 32 years old.

3. Multidimensional array:

In this example, we create a multidimensional array with automatically assigned numeric ID keys:

$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&#63;"; 

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.

<&#63;php 
$names = array("Peter","Joe","Lily"); 
for($id=0;$id<count($names);++$id) 
{ 
echo $names[$id]; 
} 
&#63;> 

2. foreach traversal

You can traverse the index array or the associative array

Traverse the index array

foreach(array_expression as $value) 
{ 
循环体; 
} 
遍历关联数组 
foreach(array_expression as $key=>$value) 
{ 
循环体; 
} 

A. One-dimensional array traversal

Index array

<&#63;php 
$contact = array("李某","xx公司","abc@xx.com"); 
foreach($contact as $value) 
{ 
echo $value; 
} 
&#63;> 
<&#63;php 
$contact = array("姓名"=>"李某","公司"=>"xx公司","邮箱"=>"abc@xx.com"); 
foreach($contact as $key=>$value) 
{ 
echo $key.":".$value; 
} 
&#63;> 

B. Multi-dimensional array traversal

<&#63;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 */
&#63;> 

The above is a detailed explanation of the examples of php arrays introduced by the editor. I hope it will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn