php copying array elements by value, not by reference

前端 未结 6 2003
清歌不尽
清歌不尽 2020-12-06 05:48

I have the following code:

$data[\'x\'] = $this->x->getResults();  

$data[\'y\'] = $data[\'x\'];

//some code here to modify $data[\'y\']
//this cause         


        
6条回答
  •  盖世英雄少女心
    2020-12-06 06:19

    array_flip() won't work when array values are not strings nor integers. I found a simple solution, however:

    $clonedArr = (array)clone(object)$arr;
    

    This works thanks to the properties of clone on an object.

提交回复
热议问题