单个元素的引用行为与数组容器的引用状态分离? [英] The reference behavior of individual elements is dissociated from the reference status of the array container?
问题描述
这是什么意思换句话说,数组的引用行为是在逐个元素的基础上定义的;单个元素的引用行为与数组容器的引用状态无关."
What does this mean "In other words, the reference behavior of arrays is defined in an element-by-element basis; the reference behavior of individual elements is dissociated from the reference status of the array container."
单个元素如何与数组容器的引用状态分离?我正在深入doc,但我很困惑这意味着什么?>
How individual elements is dissociated from the reference status of the array container? I was going thorough doc and i am confused what it means?
<?php
/* Assignment of scalar variables */
$a = 1;
$b =& $a;
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
/* Assignment of array variables */
$arr = array(1);
$a =& $arr[0]; //$a and $arr[0] are in the same reference set
$arr2 = $arr; //not an assignment-by-reference!
$arr2[0]++;
/* $a == 2, $arr == array(2) */
/* The contents of $arr are changed even though it's not a reference! */
?>
推荐答案
我想如果你使用一些 ASCII 艺术,你可能会更好地理解它:
I think if you use some ASCII art you might understand it better:
//Line 01
$a = 1;
----------
┌──────┐ ┌─────┐
│ $a │ ─────────────────> │ 1 │
└──────┘ └─────┘
//Line 02
$b = &$a;
----------
┌──────┐ ┌─────┐
│ $a │ ─────────────────> │ 1 │
└──────┘ └─────┘
^
┌──────┐ │
│ $b │ ───────────────────────┘
└──────┘
//Line 03
$c = $b;
----------
┌──────┐ ┌─────┐
│ $a │ ─────────────────> │ 1 │
└──────┘ └─────┘
^
┌──────┐ │
│ $b │ ───────────────────────┘
└──────┘
┌──────┐ ┌─────┐
│ $c │ ─────────────────> │ 1 │
└──────┘ └─────┘
//Line 04
$c = 7;
----------
┌──────┐ ┌─────┐
│ $a │ ─────────────────> │ 1 │
└──────┘ └─────┘
^
┌──────┐ │
│ $b │ ───────────────────────┘
└──────┘
┌──────┐ ┌─────┐
│ $c │ ─────────────────> │ 7 │
└──────┘ └─────┘
现在您可以看到,当您通过引用分配变量并更改它时,它也会更改另一个变量的值.这里以$a
和$b
为例,如果给$b
赋值5,$a
也会指向到 5,反之亦然.
Now as you can see when you assign a variable by reference and you change it, it will also change the value for the other variable. Here for example $a
and $b
, if you assign 5 to $b
, $a
will also point to 5, vice versa.
但是您没有通过引用将 $b
分配给 $c
(您进行了正常的按值分配),因此如果您更改了 的值$c
它不会改变 $b
( 或 $a
) 的值.
But you didn't assigned $b
to $c
by reference (you made a normal by value assignment), so if you change the value of $c
it won't change the value of $b
(, or $a
).
//Line 01
$arr = array(1);
----------
┌───────────┐ ┌─────┐
│ $arr[0] │ ─────────────────> │ 1 │
└───────────┘ └─────┘
//Line 02
$a = &$arr[0];
----------
┌───────────┐ ┌─────┐
│ $arr[0] │ ─────────────────> │ 1 │
└───────────┘ └─────┘
^
┌──────┐ │
│ $a │ ────────────────────────────┘
└──────┘
//Line 03
$arr2 = $arr;
----------
┌───────────┐ ┌─────┐
│ $arr[0] │ ─────────────────> │ 1 │
└───────────┘ └─────┘
^
┌──────┐ │
│ $a │ ────────────────────────────┤
└──────┘ │
│
┌────────────┐ │
│ $arr2[0] │ ──────────────────────┘
└────────────┘
//Line 04
$arr2[0]++;
----------
┌───────────┐ ┌─────┐
│ $arr[0] │ ─────────────────> │ 2 │
└───────────┘ └─────┘
^
┌──────┐ │
│ $a │ ────────────────────────────┤
└──────┘ │
│
┌────────────┐ │
│ $arr2[0] │ ──────────────────────┘
└────────────┘
现在是手册试图解释的内容:
Now here comes the line what the manual is trying to explain:
$arr2 = $arr;
即使您没有通过引用 $arr2
来分配数组 $arr
,该数组仍然包含一个指向引用的元素!并且该引用仍将在 $arr2
中,因此第二个数组的第一个元素也指向引用为 $arr[0]
和 $a
确实如此.
Even though you don't assign the array $arr
by reference to $arr2
, the array still holds an element which points to a reference! And that reference will still be in $arr2
, so the first element of the second array also points to the reference as $arr[0]
and $a
does.
也许如果您看到通过引用分配数组和数组包含带有引用的元素时的区别,您会更好地理解它:
Maybe if you see the difference when an array is assigned by reference and when an array holds an element with a reference you understand it better:
//Line 01
$arr1 = [1, 1, 1];
----------
┌─────────┐
│ $arr1 │
└─────────┘
│
│
└─────────> ┌────────────┐
│ Array │ ┌─────┐
│ container: │ ┌───> │ 1 │
├────────────┤ │ └─────┘
│ [0] │ ───┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 1 │
├────────────┤ └─────┘
│ [2] │ ───┐
└────────────┘ │ ┌─────┐
└───> │ 1 │
└─────┘
//Line 02
$arr2 = &$arr1;
----------
┌─────────┐
│ $arr1 │
└─────────┘
│
│
└─────────> ┌────────────┐
│ Array │ ┌─────┐
│ container: │ ┌───> │ 1 │
├────────────┤ │ └─────┘
│ [0] │ ───┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 1 │
├────────────┤ └─────┘
│ [2] │ ───┐
┌─────────> └────────────┘ │ ┌─────┐
│ └───> │ 1 │
│ └─────┘
┌─────────┐
│ $arr2 │
└─────────┘
//Line 03 & 04
$arr2[0] = 2;
$arr2[1] = 2;
----------
┌─────────┐
│ $arr1 │
└─────────┘
│
│
└─────────> ┌────────────┐
│ Array │ ┌─────┐
│ container: │ ┌───> │ 2 │
├────────────┤ │ └─────┘
│ [0] │ ───┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 2 │
├────────────┤ └─────┘
│ [2] │ ───┐
┌─────────> └────────────┘ │ ┌─────┐
│ └───> │ 1 │
│ └─────┘
┌─────────┐
│ $arr2 │
└─────────┘
正如您在此处看到的,由于您通过引用将 $arr1
分配给 $arr2
,它们都指向同一个数组.
So as you can see here, since you assigned $arr1
to $arr2
by reference they both point to the same array.
//Line 01
$a = 1;
----------
┌──────┐ ┌─────┐
│ $a │ ─────────────────> │ 1 │
└──────┘ └─────┘
//Line 02
$arr3 = [&$a, 1, 1];
----------
┌──────┐ ┌─────┐
│ $a │ ──────────────────> │ 1 │
└──────┘ └─────┘
^
┌─────────┐ │
│ $arr3 │ │
└─────────┘ │
│ │
│ │
└─────────> ┌────────────┐ │
│ Array │ │
│ container: │ │
├────────────┤ │
│ [0] │ ───┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 1 │
├────────────┤ └─────┘
│ [2] │ ───┐
└────────────┘ │ ┌─────┐
└───> │ 1 │
└─────┘
//Line 03
$arr4 = $arr3;
----------
┌──────┐ ┌─────┐
│ $a │ ──────────────────> │ 1 │ <─────────┐
└──────┘ └─────┘ │
^ │
┌─────────┐ │ │
│ $arr3 │ │ │
└─────────┘ │ │
│ │ │
│ │ │
└─────────> ┌────────────┐ │ │
│ Array │ │ │
│ container: │ │ │
├────────────┤ │ │
│ [0] │ ───┘ │
├────────────┤ ┌─────┐ │
│ [1] │ ───────> │ 1 │ │
├────────────┤ └─────┘ │
│ [2] │ ───┐ │
└────────────┘ │ ┌─────┐ │
└───> │ 1 │ │
└─────┘ │
│
┌─────────┐ │
│ $arr4 │ │
└─────────┘ │
│ │
│ │
└─────────> ┌────────────┐ │
│ Array │ │
│ container: │ │
├────────────┤ │
│ [0] │ ──────────────────┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 1 │
├────────────┤ └─────┘
│ [2] │ ───┐
└────────────┘ │ ┌─────┐
└───> │ 1 │
└─────┘
//Line 03 & 04
$arr4[0] = 2;
$arr4[1] = 2;
----------
┌──────┐ ┌─────┐
│ $a │ ──────────────────> │ 2 │ <─────────┐
└──────┘ └─────┘ │
^ │
┌─────────┐ │ │
│ $arr3 │ │ │
└─────────┘ │ │
│ │ │
│ │ │
└─────────> ┌────────────┐ │ │
│ Array │ │ │
│ container: │ │ │
├────────────┤ │ │
│ [0] │ ───┘ │
├────────────┤ ┌─────┐ │
│ [1] │ ───────> │ 1 │ │
├────────────┤ └─────┘ │
│ [2] │ ───┐ │
└────────────┘ │ ┌─────┐ │
└───> │ 1 │ │
└─────┘ │
│
┌─────────┐ │
│ $arr4 │ │
└─────────┘ │
│ │
│ │
└─────────> ┌────────────┐ │
│ Array │ │
│ container: │ │
├────────────┤ │
│ [0] │ ──────────────────┘
├────────────┤ ┌─────┐
│ [1] │ ───────> │ 2 │
├────────────┤ └─────┘
│ [2] │ ───┐
└────────────┘ │ ┌─────┐
└───> │ 1 │
└─────┘
正如您在此处看到的,即使您通过值而不是引用将 $arr3
分配给 $arr4
!该数组仍包含该引用,该引用与 $a
和 $arr3[0]
共享.
So as you can see here even though you assigned $arr3
to $arr4
by value, not by reference! The array still contains that reference, which is shared with $a
and $arr3[0]
.
这篇关于单个元素的引用行为与数组容器的引用状态分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!