Table of Contents
回复内容:

PHP数组值问题

Jun 06, 2016 pm 08:32 PM
php

直接上代码吧。希望可以得到大神的分析结果。

<code>$test = array(1, 2, 3, 4, 5);
foreach ($test as &amp;$value) {
    $value = 0;
}
unset($value);
print_r($test);

// 结果如下
Array
(
    [0] =&gt; 0
    [1] =&gt; 0
    [2] =&gt; 0
    [3] =&gt; 0
    [4] =&gt; 0
)

$test = array(1, 2, 3, 4, 5);
foreach ((array)$test as &amp;$value) {
    $value = 0;
}
unset($value);
print_r($test);

// 结果如下
Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
    [4] =&gt; 5
)
</code>
Copy after login
Copy after login

如上代码所示,仅仅是使用了一下数组强制类型转换,结果却是截然不同,我想知道为什么?

回复内容:

直接上代码吧。希望可以得到大神的分析结果。

<code>$test = array(1, 2, 3, 4, 5);
foreach ($test as &amp;$value) {
    $value = 0;
}
unset($value);
print_r($test);

// 结果如下
Array
(
    [0] =&gt; 0
    [1] =&gt; 0
    [2] =&gt; 0
    [3] =&gt; 0
    [4] =&gt; 0
)

$test = array(1, 2, 3, 4, 5);
foreach ((array)$test as &amp;$value) {
    $value = 0;
}
unset($value);
print_r($test);

// 结果如下
Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
    [4] =&gt; 5
)
</code>
Copy after login
Copy after login

如上代码所示,仅仅是使用了一下数组强制类型转换,结果却是截然不同,我想知道为什么?

先看这个例子

<code>$test = array(1, 2, 3, 4, 5);

$test1 = $test;

foreach ($test1 as &amp;$value) {
    $value = 0;
}

print_r($test);

Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
    [4] =&gt; 5
)
</code>
Copy after login

再看这个例子

<code>$test = array(1, 2, 3, 4, 5);

$test1 = &amp;$test;

foreach ($test1 as &amp;$value) {
    $value = 0;
}

print_r($test);

Array
(
    [0] =&gt; 0
    [1] =&gt; 0
    [2] =&gt; 0
    [3] =&gt; 0
    [4] =&gt; 0
)
</code>
Copy after login

上面连个例子都是先赋值,然后再执行foreach,但是第一个是传值,而第二个是传引用(地址),传值相对于对原array 做了一个copy 赋值到新的变量上,他们在内存中指向不是同一个地方。

在看你的第二个例子中的 (array)$test, 这个其实相当于创建一个新的array,这个array 和test 肯定不是指向同一个内存地址,所以你foreach中的赋值操作并不会修改原来test变量中的值

(array)$test 这本身是一个赋值操作 等于 $a = (array)$test 然后再 foreach $a

数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符&通过引用来拷贝数组

<code>php</code><code>$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]
</code>
Copy after login

foreach遍历数组的时候,不加引用他每遍历一次都会释放掉value得值,如果加了引用,他不会释放,而被后来的给覆盖掉[图片]要相信我,

两种代码我输出opcode,来针对opcode做讨论.
在做foreach(xxx as xx)这类操作的时候,调用的指令是FE_REST来重置数组的迭代器.
从我截图的红框里可以清楚的看到,第一次重置的时候用的是!0,也就是$test,第二次用的是~2,也就是(array)$test.
所以你第一次更改的时候,改变的是$test里的内容,而第二次更改的时候,改变的是(array)$test这个变量里的内容,意思就是第二次根本就没更改到$test
PHP数组值问题

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles