PHP Array Instance _php example

Source: Internet
Author: User
Tags arrays

As a C + + programmer, in the process of PHP development, the PHP array produced some confusion, and C + + array has a similar place, there are some differences, the following is a comprehensive analysis of the PHP array and its corresponding data types in C + + and the difference and contact.

Classification of arrays:

1, the numerical array: Also called the index array, that is, the number (starting from 0) as an array subscript. The equivalent vector in C + +.

2. Associative array: Subscript as an array of strings. Equivalent to the map in C + +.

3. Multidimensional array: Each element in an array is also an array. Each element in its child array can also be an array.

The declaration of an array:

1. Numerical array

A, in the following example, the number ID key is assigned automatically.

$names = Array ("Peter", "Joe", "Lily");
b, in the following example, we manually assign the number ID key.

$names [0] = "Peter";
$names [1] = "Joe";
$names [2] = "Lily";

You can use these ID keys in your scripts:

<?php 
$names [0] = "Peter"; 
$names [1] = "Joe"; 
$names [2] = "Lily"; 
echo $names [0]. "and". $names [1]. "Are". $names [2]. "' S neighbors "; 
* * How to ask hovertree.com * *

2. Associative array:

Example 1

$ages = Array ("Peter" =>32, "Joe" =>30, "Lily" =>28);

Example 2

This example is the same as Example 1 and is just another way to create an array.

$ages ["Peter"] = "the"; 
$ages ["Joe"] = "30"; 

To use an associative array in a script:

<?php 
$ages ["Peter"] = "the"; 
$ages ["Joe"] = "a"; 
$ages ["Lily"] = "a"; 
Echo ' Peter is '. $ages ["Peter"]. "Years old."; 
* * How to ask hovertree.com * *

Above script output:

Peter is years old.

3, multidimensional array:

In this example, we created a multidimensional array with the auto assign number ID key:

$families = array 
{ 
"Griffin" =>array 
{" 
Peter", 
"Lois", 
"Megan" 
}, 
" Quagmire "=>array 
{" 
Glenn " 
}, 
" Brown "=>array 
{" 
Cleveland ", 
" Loretta " , 
"Junior" 
} 
}; 

Above code output:

Is Megan a part of the Griffin family?

1, for Loop traversal

The 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, foreach Traversal

That is, you can traverse an indexed array, or you can traverse an associative array

Traversing an indexed array

foreach (array_expression as $value) 
{ 
loop body; 
} 
Traverses the associative array 
foreach (array_expression as $key => $value) 
{the 
loop body; 

A, one-dimensional array traversal

Indexed arrays

<?php 
$contact = Array ("Li MoU", "xx Company", "abc@xx.com"); 
foreach ($contact as $value) 
{ 
echo $value; 
} 
? > 
<?php 
$contact = Array ("name" => "Li MoU", "Company" => "xx Company", "Mailbox" => "abc@xx.com"); 
foreach ($contact as $key => $value) 
{ 
echo $key. ":". $value; 
} 

B, multidimensional array traversal

<?php 
$wage = Array ( 
"Marketing" =array ( 
1, "Li", "Marketing Manager", 8000), 
array (2, "King", "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 Commissioner", 5000) 
), 
"accounts Department" =array ( 
Array (1, "Li MoU", "Account Manager", 7000), 
Array (2, "Wang MoU", "Account Officer", 6000), 
array (3, "Liu MoU", "Account Commissioner", 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 *

The above is a small series to introduce the example of PHP array of examples, I hope to help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.