PHP array instance details, php array details _ PHP Tutorial

Source: Internet
Author: User
Tags array example
PHP array instance details, php array details. PHP array example explanation, php array explanation as a C ++ programmer, some obfuscation occurs on the PHP array during the PHP development process, similar to the C ++ array, there are some examples of non-PHP arrays, and php arrays

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
  
 

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.

As a C ++ programmer, pipeline produces some obfuscation on the PHP array during the PHP development process, which is similar to the C ++ array, there are also some not...

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.