PHP Array Value problem

Source: Internet
Author: User
Go directly to the code. Hope can get the analysis result of the great God.

$test = array(1, 2, 3, 4, 5);foreach ($test as &$value) {    $value = 0;}unset($value);print_r($test);// 结果如下Array(    [0] => 0    [1] => 0    [2] => 0    [3] => 0    [4] => 0)$test = array(1, 2, 3, 4, 5);foreach ((array)$test as &$value) {    $value = 0;}unset($value);print_r($test);// 结果如下Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5)

As shown in the code above, just using the array coercion type conversion, the result is very different, I want to know why?

Reply content:

Go directly to the code. Hope can get the analysis result of the great God.

$test = array(1, 2, 3, 4, 5);foreach ($test as &$value) {    $value = 0;}unset($value);print_r($test);// 结果如下Array(    [0] => 0    [1] => 0    [2] => 0    [3] => 0    [4] => 0)$test = array(1, 2, 3, 4, 5);foreach ((array)$test as &$value) {    $value = 0;}unset($value);print_r($test);// 结果如下Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5)

As shown in the code above, just using the array coercion type conversion, the result is very different, I want to know why?

Let's look at this example.

$test = array(1, 2, 3, 4, 5);$test1 = $test;foreach ($test1 as &$value) {    $value = 0;}print_r($test);Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5)

Look at this example again.

$test = array(1, 2, 3, 4, 5);$test1 = &$test;foreach ($test1 as &$value) {    $value = 0;}print_r($test);Array(    [0] => 0    [1] => 0    [2] => 0    [3] => 0    [4] => 0)

Above even an example is the first assignment, then the foreach, but the first is a value, and the second is a reference (address), the value of the original array to do a copy assignment to the new variable, they point in memory is not the same place.

In the second example of your (array) $test, this is actually the equivalent of creating a new array, this array and test must not point to the same memory address, so the assignment operation in foreach does not modify the value in the original test variable

(array) $test this itself is an assignment operation equal to $a = (array) $test and then the foreach $a

The assignment of arrays (array) always involves a copy of the value. Copy an array using the reference operator & by reference

php$test = array(1, 2, 3, 4, 5);$test2 = (array)$test;$test2[0] = 0;print_r($test);//[1,2,3,4,5]print_r($test2);//[0,2,3,4,5]

When a foreach iterates through an array, he will release value for every traversal, and if you add a reference, he will not release it and be overwritten by the subsequent one. Believe me,

Two kinds of code i output opcode to discuss for opcode.
When doing such operations as foreach (xxx as XX), the invoked instruction is fe_rest to reset the array's iterator.
From my red box can be clearly seen, the first time to reset the use of the! 0, that is, $test, the second use of a, that is (array) $test.
So the first time you change, you change the contents of the $test, and the second time, the change is (array) $test the contents of this variable, meaning that the second time is not changed to $test

  • 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.