[PHP-NOTES] note 130865 added to language.types.array
[email protected] ("Anonymous")
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
Note that holding a reference to array element changes the behavior of "copying" the array:
<?php
$original = array("data" => "original value");
$element_reference = &$original["data"]; // $element_reference is not used, just hold!
$copy = $original;
$copy["data"] = "value set in copy";
var_dump($original);
?>
results in
array(1) {
["data"]=>
&string(17) "value set in copy"
}
If I've understood correctly, taking the reference modifies the original array to now contain a reference to hidden (potentially shared) data and copying the array will copy the reference instead of copying the data and the original reference simply pointing to given array element without modifying the original array.
Example: https://3v4l.org/jePdD#veol
----
Server IP: 2a03:b0c0:2:f0:0:1:a983:a001 (proxied: BunnyCDN)
Probable Submitter: 86.60.202.83
----
Manual Page -- https://php.net/manual/en/language.types.array.php
Edit -- https://main.php.net/note/edit/130865
Del: integrated -- https://main.php.net/note/delete/130865/integrated
Del: useless -- https://main.php.net/note/delete/130865/useless
Del: bad code -- https://main.php.net/note/delete/130865/bad+code
Del: spam -- https://main.php.net/note/delete/130865/spam
Del: non-english -- https://main.php.net/note/delete/130865/non-english
Del: in docs -- https://main.php.net/note/delete/130865/in+docs
Del: other reasons-- https://main.php.net/note/delete/130865
Reject -- https://main.php.net/note/reject/130865
Search -- https://main.php.net/manage/user-notes.php