php按值复制数组元素,而不是按引用 [英] php copying array elements by value, not by reference

查看:33
本文介绍了php按值复制数组元素,而不是按引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$data['x'] = $this->x->getResults();$data['y'] = $data['x'];//这里有一些代码来修改 $data['y']//这会导致(不希望的)$data['x] 也被修改

我猜因为 $data 的所有元素本身都是引用,所以修改 $data['y'] 也会修改 $data['x'] ..这不是我想要的.我希望 $data['x'] 保持不变.有什么方法可以取消引用这里的元素,以便我可以按值复制元素?

谢谢.

更新:$this->x->getResults();返回一个对象数组.所以我可以做这样的事情: $data['x'][0]->date_create ...

更新:我最近克隆阵列的尝试如下所示:

 $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 方法"错误.从阅读响应来看,我的最佳选择似乎是迭代每个元素并克隆它(这就是我试图用该代码做的事情......).

更新:刚刚解决了!:在 foreach 循环中,我只需要将行更改为:

$data['y'][$key] = clone $row;

它有效!感谢大家的帮助.

解决方案

您可以利用 PHP 将取消对函数调用结果的引用这一事实.

这是我编写的一些示例代码:

$x = 'x';$y = 'y';$arr = array(&$x,&$y);打印_r($arr);回声<br/>";$arr2 = $arr;$arr2[0] = 'zzz';打印_r($arr);打印_r($arr2);回声<br/>";$arr2 = array_flip(array_flip($arr));$arr2[0] = '123';打印_r($arr);打印_r($arr2);

结果如下:

<前>数组 ( [0] => x [1] => y )数组 ( [0] => zzz [1] => y ) 数组 ( [0] => zzz [1] => y )数组 ( [0] => zzz [1] => y ) 数组 ( [0] => 123 [1] => y )

可以看到,在将$arr赋值给$arr2的过程中使用array_flip()的结果导致了后续的不同更改为 $arr2,因为 array_flip() 调用会强制取消引用.

这看起来效率不高,但如果 $this->x->getResults() 返回一个数组,它可能对你有用:

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

请参阅这个(未回答的)线程以获取另一个示例.

如果返回的数组中的所有内容都是对象,那么复制对象的唯一方法是使用 clone(),并且您必须遍历 $data['x'] 并将每个元素克隆到 $data['y'] 中.

示例:

$data['x'] = $this->x->getResults();$data['y'] = array();foreach($data['x'] as $key => $obj) {$data['y'][$key] = 克隆 $obj;}

I have the following 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

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?

Thanks.

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;
   }

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..).

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.

解决方案

You can take advantage of the fact that PHP will dereference the results of a function call.

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);

The results look like this:

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 ) 

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.

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'];

See this (unanswered) thread for another example.

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'].

Example:

$data['x'] = $this->x->getResults();
$data['y'] = array();
foreach($data['x'] as $key => $obj) {
    $data['y'][$key] = clone $obj;
}

这篇关于php按值复制数组元素,而不是按引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆