When you assign-by-value an array variable in PHP, is the array really deep-copied?

孤街浪徒 提交于 2019-12-13 10:36:45

问题


It seems really inefficient that in PHP, when an array is assigned by value all of its internal elements are recursively copied to the new variable. Is this what really happens internally?


回答1:


No, the internally array is not deep-copied on assignment. Consider the following snippet:

$a = array(111, 222, 333);
$b = $a;
$b[0] = 999;

If a picture is worth a thousand words, then here is what happens internally when arrays are assigned and then their array elements are modified:



来源:https://stackoverflow.com/questions/28280833/when-you-assign-by-value-an-array-variable-in-php-is-the-array-really-deep-copi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!