Title: The reference behavior of individual elements is dissociated from the reference status of the array container?
Tags: php;reference;pass-by-reference
Question: 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."
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 =&amp; $a;
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
/* Assignment of array variables */
$arr = array(1);
$a =&amp; $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! */
Comment: `$arr2 = $arr;` You don't assign it by reference, but the array contains an element which has the same reference as `$a` and this still applies even though you assign the array by value.

Comment: Thanks @Rizier123 got it somewhat you are saying. more explanation would be helpful.

Comment: Did not know about christophernelson@example.org. Will be glad to see some clarification

Here is the accepted answer: I think if you use some ASCII art you might understand it better:
First example:
//Line 01
$a = 1;
----------
┌──────┐                       ┌─────┐
│  $a  │   ─────────────────&gt;  │  1  │
└──────┘                       └─────┘
//Line 02
$b = &amp;$a;
----------
┌──────┐                       ┌─────┐
│  $a  │   ─────────────────&gt;  │  1  │
└──────┘                       └─────┘
^
┌──────┐                          │
│  $b  │   ───────────────────────┘
└──────┘
//Line 03
$c = $b;
----------
┌──────┐                       ┌─────┐
│  $a  │   ─────────────────&gt;  │  1  │
└──────┘                       └─────┘
^
┌──────┐                          │
│  $b  │   ───────────────────────┘
└──────┘
┌──────┐                       ┌─────┐
│  $c  │   ─────────────────&gt;  │  1  │
└──────┘                       └─────┘
//Line 04
$c = 7;
----------
┌──────┐                       ┌─────┐
│  $a  │   ─────────────────&gt;  │  1  │
└──────┘                       └─────┘
^
┌──────┐                          │
│  $b  │   ───────────────────────┘
└──────┘
┌──────┐                       ┌─────┐
│  $c  │   ─────────────────&gt;  │  7  │
└──────┘                       └─────┘
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.
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).
Second example:
//Line 01
$arr = array(1);
----------
┌───────────┐                       ┌─────┐
│  $arr[0]  │   ─────────────────&gt;  │  1  │
└───────────┘                       └─────┘
//Line 02
$a = &amp;$arr[0];
----------
┌───────────┐                       ┌─────┐
│  $arr[0]  │   ─────────────────&gt;  │  1  │
└───────────┘                       └─────┘
^
┌──────┐                               │
│  $a  │   ────────────────────────────┘
└──────┘
//Line 03
$arr2 = $arr;
----------
┌───────────┐                       ┌─────┐
│  $arr[0]  │   ─────────────────>  │  1  │
└───────────┘                       └─────┘
^
┌──────┐                               │
│  $a  │   ────────────────────────────┤
└──────┘                               │
│
┌────────────┐                         │
│  $arr2[0]  │   ──────────────────────┘
└────────────┘
//Line 04
$arr2[0]++;
----------
┌───────────┐                       ┌─────┐
│  $arr[0]  │   ─────────────────&gt;  │  2  │
└───────────┘                       └─────┘
^
┌──────┐                               │
│  $a  │   ────────────────────────────┤
└──────┘                               │
│
┌────────────┐                         │
│  $arr2[0]  │   ──────────────────────┘
└────────────┘
Now here comes the line what the manual is trying to explain:
$arr2 = $arr;
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:
Array assigned by reference:
//Line 01
$arr1 = [1, 1, 1];
----------
┌─────────┐
│  $arr1  │
└─────────┘
│
│
└─────────&gt; ┌────────────┐
│   Array    │          ┌─────┐
│ container: │    ┌───&gt; │  1  │
├────────────┤    │     └─────┘
│    [0]     │ ───┘
├────────────┤          ┌─────┐
│    [1]     │ ───────&gt; │  1  │
├────────────┤          └─────┘
│    [2]     │ ───┐
└────────────┘    │     ┌─────┐
└───&gt; │  1  │
└─────┘
//Line 02
$arr2 = &$arr1;
----------
┌─────────┐
│  $arr1  │
└─────────┘
│
│
└─────────> ┌────────────┐
│   Array    │          ┌─────┐
│ container: │    ┌───> │  1  │
├────────────┤    │     └─────┘
│    [0]     │ ───┘
├────────────┤          ┌─────┐
│    [1]     │ ───────> │  1  │
├────────────┤          └─────┘
│    [2]     │ ───┐
┌─────────> └────────────┘    │     ┌─────┐
│                             └───> │  1  │
│                                   └─────┘
┌─────────┐
│  $arr2  │
└─────────┘
//Line 03 &amp; 04
$arr2[0] = 2;
$arr2[1] = 2;
----------
┌─────────┐
│  $arr1  │
└─────────┘
│
│
└─────────&gt; ┌────────────┐
│   Array    │          ┌─────┐
│ container: │    ┌───&gt; │  2  │
├────────────┤    │     └─────┘
│    [0]     │ ───┘
├────────────┤          ┌─────┐
│    [1]     │ ───────&gt; │  2  │
├────────────┤          └─────┘
│    [2]     │ ───┐
┌─────────&gt; └────────────┘    │     ┌─────┐
│                             └───&gt; │  1  │
│                                   └─────┘
┌─────────┐
│  $arr2  │
└─────────┘
So as you can see here, since you assigned $arr1 to $arr2 by reference they both point to the same array.
Array containing a reference:
//Line 01
$a = 1;
----------
┌──────┐                       ┌─────┐
│  $a  │   ─────────────────&gt;  │  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 &amp; 04
$arr4[0] = 2;
$arr4[1] = 2;
----------
┌──────┐                        ┌─────┐
│  $a  │   ──────────────────&gt;  │  2  │ <─────────┐
└──────┘                        └─────┘           │
^              │
┌─────────┐                        │              │
│  $arr3  │                        │              │
└─────────┘                        │              │
│                             │              │
│                             │              │
└─────────&gt; ┌────────────┐    │              │
│   Array    │    │              │
│ container: │    │              │
├────────────┤    │              │
│    [0]     │ ───┘              │
├────────────┤          ┌─────┐  │
│    [1]     │ ───────&gt; │  1  │  │
├────────────┤          └─────┘  │
│    [2]     │ ───┐              │
└────────────┘    │     ┌─────┐  │
└───&gt; │  1  │  │
└─────┘  │
│
┌─────────┐                                       │
│  $arr4  │                                       │
└─────────┘                                       │
│                                            │
│                                            │
└─────────&gt; ┌────────────┐                   │
│   Array    │                   │
│ container: │                   │
├────────────┤                   │
│    [0]     │ ──────────────────┘
├────────────┤          ┌─────┐
│    [1]     │ ───────&gt; │  2  │
├────────────┤          └─────┘
│    [2]     │ ───┐
└────────────┘    │     ┌─────┐
└───&gt; │  1  │
└─────┘
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].
Comment for this answer: @kamlesh.bar Maybe this example makes it a bit more clearer: https://3v4l.org/la5El
Comment for this answer: @kamlesh.bar You're welcome. Updated my answer again and made a separate example.
Comment for this answer: Bravo Man :) got your point completely. Amazed by strange behaviour


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:12/03/15', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><reference><pass-by-reference>', 'Title': 'The reference behavior of individual elements is dissociated from the reference status of the array container?', 'ViewCount': 65, 'id': 34065928, 'user_AboutMe': '<p>Skype : kamlesh.bar</p>\n\n<p>Email: barkamlesh@gmail.com</p>\n\n<p>CAKE PHP | Laravel | Zend Framework 2 | CodeIgniter | Joomla | Wordpress | Prestashop | PHP Developer | Jquery | AJAX | HTML | CSS</p>\n\n<p>Web Service developer, iPhone APNS, ANDROID GCM Notification</p>\n', 'user_AccountId': 1618417.0, 'user_DisplayName': 'kamlesh.bar', 'user_Reputation': 1737.0, 'user_Views': 397.0, 'user_id': 1495497.0}}