Measure the test taker's knowledge about Array, foreach, and arrayforeach in PHP.

Source: Internet
Author: User
Tags array definition

Measure the test taker's knowledge about Array, foreach, and arrayforeach in PHP.

1. Understand Arrays

Arrays in PHP are actually an ordered ing. Ing isValuesAssociatedKeys. For more information, see Array in PHP.net.

2. Example: A general Array

Here, I use a simple example and use the graphical method to understand arrays.

//1.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo var_dump($a);

[Note]: use arrows to describe the data values of each unit in array $ a corresponding to a memory address (in fact, its internal structure adopts the HashTable structure, refer to the Hash algorithm in PHP written by laruence ).

 

3. Example: Add a reference to the array definition.

//2.-----------------------------------$x = 'x';$a = array(3 => 'a', 1 => &$x, 2 => 'c');echo "

$ A [1] and $ x correspond to the same data. When var_dump ($ a) is used, multiple & symbols (& string (1) "x") of the array's 2nd units are displayed, indicating reference.

When you change the value of $ x to 'y', it is equivalent to modifying the value of $ a [1] to 'y '.

You can clearly describe this change:

4. Example: Use foreach to traverse the array.

//3.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo "

In each loop, the unit value in the current array is assigned to $ value.KeyThe key is assigned to $ key. As described in:

[Note:] The gray virtual arrow indicates giving a value.

 

5. Example: Use the reference value assignment in the foreach traversal array.

//4.-----------------------------------$a = array(3 => 'a', 1 => 'b', 2 => 'c');echo "

In each loop, $ value points to the value of the unit in the current array, and then executes the code "$ value. = 'n';", as described in:

 

 

6. Example: further analysis of Example 5.

In Example 5, after foreach traverses the array, the $ value variable is not automatically destroyed, pointing to the same data as the last unit $ a [2] of array $.

At this time, the value of $ value is changed, that is, the value of $ a [2] is changed.

//5.-----------------------------------$value='m';echo "

 

Instance verification, the last element of the array$ ValueReference inForeachIt is retained after the loop. We recommend that you use unset () to destroy it.

 

7. Summary

The above example only describes some features of array and foreach in php. Finally, I feel that the array and foreach in php are different from other programming languages. PHP cannot be analyzed using a structure similar to the C language.

 

(End .)

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.