我们有以下代码:
$fruits = array(
(object) array(
'name' => 'orange',
'country' => 'brazil',
'qty' => 23,
'destination' => 'romania'
),
(object) array(
'name' => 'pinapple',
'country' => 'thailand',
'qty' => 3,
'destination' => 'germany'
),
(object) array(
'name' => 'carrot',
'country' => 'romania',
'qty' => 250,
'destination' => 'france'
),
(object) array(
'name' => 'bannana',
'country' => 'kenya',
'qty' => 50,
'destination' => 'sweden'
),
(object) array(
'name' => 'melon',
'country' => 'romania',
'qty' => 50,
'destination' => 'uae'
)
);
function testing($fruits)
{
for($i = 0, $size = sizeof($fruits); $i < $size; $i++)
{
$fruit = $fruits[$i];
$fruits[$i]->qty = $fruit->qty * 100;
unset($fruit->destination);
unset($fruit->test);
unset($fruit->country);
}
return $fruits;
}
var_dump($fruits);
testing($fruits);
var_dump($fruits);
?>
如果您运行该代码,$ fruits数组的项目已更改,因为它已作为参考传递。
问题是:我们如何传递它以便tests()函数不会改变$ fruits?
感谢所有人。
答案 0 :(得分:0)
如何克隆你的对象?
$clone = array();
foreach ($fruits as $k => $v) {
$clone[$k] = clone $v;
}
testing($clone);