Deep understanding of array arrays and foreach_php instances in PHP

Source: Internet
Author: User
Tags array definition php and

1. Understanding Arrays

The array in PHP is actually an ordered map. A mapping is a type that associates values to the keys. For a detailed explanation see: Array arrays in php.net.

2. Example: General array

Here, I go through a simple example and use the graphical way to understand the array.

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

echo var_dump ($a);

[Note]: Use the arrows to describe the array $a each cell corresponding to a memory address of the data value (in fact, its internal structure using Hashtable structure, you can refer to the brother wrote PHP hash algorithm).

3. Example: In the 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 the 2nd cell of the array with multiple & symbols, that is, &string (1) "X", Represents a 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 in the following figure:

4. Example: Use foreach to traverse 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 unit is assigned to the $key. The following figure describes:

"Note:" A gray dashed arrow indicates that a value is assigned.

5. Example: In a foreach traversal array, use references to assign values.

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

echo " 
 

In each loop, the $value points to the value of the cell in the current array, and then executes the $value. = ' n '; Code , as described in the following figure:

6. Example: A further analysis of the example 5.

After the example 5,foreach traverses the array, the $value variable is not automatically destroyed, pointing to the same data as the array $a last cell $a[2.

This changes the value of the $value, which changes the value of the $a[2].

$value = ' m ';

echo " 
 

Instance validation, the $value Reference of the last element of the array remains after the foreach loop. It is recommended that you use unset () to destroy it.

7. Summary

The above example just describes the array of arrays in PHP and some of the features of foreach. Finally, I feel that the array arrays and foreach in PHP are different from other programming languages, and you can't use a structure like C to analyze PHP.

The above is a small series for you to bring in-depth understanding of the array in PHP and the entire content of foreach, I hope that we support the cloud-Habitat Community ~

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.