Get an in-depth look at array arrays and foreach in PHP

Source: Internet
Author: User
Tags array definition
1. Understanding Arrays

The array in PHP is actually an ordered map. A map is a type that associates values to the keys. A detailed explanation can be found in: array arrays in php.net.

2. Example: General array

Here, I pass a simple example and use graphical methods to understand the array.

$a = Array (3 = ' a ', 1 = ' B ', 2 = ' C '); echo var_dump ($a);

[note]: Use arrows to describe the data value of each unit of an array of $ A memory address (in fact, its internal structure using the Hashtable structure, you can refer to the bird brother written in PHP hash algorithm).

3. Example: In an array definition, add a reference.

$x = ' x '; $a = array (3 = ' a ', 1 = & $x, 2 = ' C '); echo "

The 2nd cell in the array $ A $a[1] corresponds to the same data as $x, and when you use Var_dump ($a), you see multiple & symbols for the 2nd element of the array, that is, &string (1) "X", which represents the reference.

When modifying the value of $x = ' Y ', it is also equivalent to modifying the value of $a[1] = ' y '.

This change can be clearly described:

4. Example: Use foreach to iterate through an array.

$a = Array (3 = ' a ', 1 = ' B ', 2 = ' C '); echo "

In each loop, the value of the cell in the current array is assigned to the $value, and the key of the cell is assigned to $key. As described:

Note: The gray dashed arrows indicate a value to be assigned.

5. Example: In a foreach traversal array, use a reference to assign a value.

$a = Array (3 = ' a ', 1 = ' B ', 2 = ' C '); echo "

6. Example: Further analysis of Example 5.

After the example 5,foreach iterates through the array, the $value variable is not automatically destroyed, and the last unit of the array $ A $a[2] points to the same data.

This time change the value of $value, that is, change the value of $a[2].

$value = ' m '; echo "

The instance verifies that the $value reference of the last element of the array remains after the Foreach loop. It is recommended to use unset () to destroy it.

7. Summary

The above example simply describes some of the features of array arrays and foreach in PHP. Learning Finally, it feels like array arrays and foreach in PHP are different from other programming languages, and you can't parse PHP with a C-like structure.

The above is a small series for everyone to get in-depth understanding of the array in PHP and foreach all the content, I hope that you support PHP Chinese network ~

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.