通过值的PHP复制数组元素,而不是引用
我有以下的code:
$data['x'] = $this->x->getResults();
$data['y'] = $data['x'];
//some code here to modify $data['y']
//this causes (undesirably) $data['x] to be modified as well
我猜自$数据中的所有元素本身的引用,修改$数据['Y']还修改$数据['X'] ..这不是我想要的。我想$数据['X']保持不变。有什么办法取消引用这里的要素,这样我可以通过价值元素复制?
I guess since all the elements of $data are themselves references, modifying $data['y'] also modifies $data['x']..which is NOT what I want. I want $data['x'] to remain the same. Is there any way to dereference the elements here so that I can copy the elements by value?
感谢。
更新函数:$ this-> X-> getResults();返回一个对象数组。所以,我就可以这样做:$数据['X'] [0] - > date_create ...
Update: $this->x->getResults(); returns an object array. So I can then do something like: $data['x'][0]->date_create ...
更新:
克隆阵列我的最新尝试,看起来是这样的:
Update: my latest attempt to clone the array looks something like this:
$data['x'] = $this->x->getResults();
$data['y'] = $data['y'];
foreach($data['x'] as $key=>$row) {
$data['y'][$key]->some_attr = clone $row->some_attr;
}
我是在这里下车的方式?我不断收到错误呼吁非对象__clone方法。从阅读的反应好像我最好的选择是遍历每个元素并克隆它(这是我试图与code ++做的..)。
Am I way off here? I keep getting a "__clone method called on non-object" error. From reading the responses it seems like my best option is to iterate over each element and clone it (which is what I was trying to do with that code..).
更新:只要解决了它!:foreach循环里面我只需要将该行更改为:
UPDATE: Just solved it!: inside the foreach loop I just needed to change the line to:
$data['y'][$key] = clone $row;
和它的作品!感谢大家的帮助。
And it works! Thanks to everyone for the help.
您可以采取的事实,即PHP将取消引用函数调用的结果。
You can take advantage of the fact that PHP will dereference the results of a function call.
下面是一些例子code我掀起了:
Here's some example code I whipped up:
$x = 'x';
$y = 'y';
$arr = array(&$x,&$y);
print_r($arr);
echo "<br/>";
$arr2 = $arr;
$arr2[0] = 'zzz';
print_r($arr);
print_r($arr2);
echo "<br/>";
$arr2 = array_flip(array_flip($arr));
$arr2[0] = '123';
print_r($arr);
print_r($arr2);
结果是这样的:
Array ( [0] => x [1] => y )
Array ( [0] => zzz [1] => y ) Array ( [0] => zzz [1] => y )
Array ( [0] => zzz [1] => y ) Array ( [0] => 123 [1] => y )
您可以看到,使用的结果 array_flip()
$改编
的assigment期间 $ ARR2
在随后的变化差异的结果 $ ARR2
,因为 array_flip()
要求强制取消引用。
You can see that the results of using array_flip()
during the assigment of $arr
to $arr2
results in differences in the subsequent changes to $arr2
, as the array_flip()
calls forces a dereference.
这似乎不是非常有效的,但如果 $这个 - &GT它可能为你工作; X-&GT; getResults()
将返回数组:
It doesn't seem terribly efficient, but it might work for you if $this->x->getResults()
is returning an array:
$data['x'] = array_flip(array_flip($this->x->getResults()));
$data['y'] = $data['x'];
请参阅本(未)线程另一个例子。
如果一切您返回数组中的一个对象然而,然后复制对象的唯一方法是使用的clone()
,你将不得不通过 $数据['X'] ,每个元素克隆到 $数据['Y']
。
If everything in your returned array is an object however, then the only way to copy an object is to use clone()
, and you would have to iterate through $data['x']
and clone each element into $data['y']
.
例如:
$data['x'] = $this->x->getResults();
$data['y'] = array();
foreach($data['x'] as $key => $obj) {
$data['y'][$key] = clone $obj;
}