数组在PHP中是通过引用还是值传递?

Are arrays passed by reference or value in PHP?

提问人:user4951 提问时间:8/5/2016 最后编辑:reformeduser4951 更新时间:8/23/2016 访问量:879

问:

数组在PHP中是通过引用还是值传递?

例如,让我们看看这段代码。

function addWeight($arout, $lineCountry, $lineDomain, $target, $weight)
{
    $currentDomain=getDomain();
    $currentCountry=getCountry();

    if ($currentCountry==$lineCountry && ($currentDomain == $lineDomain || $lineDomain==""))
    {
        $tarobreakpoint=0;
        $arout [$target] =  intval($weight);
    }

    return $arout;
}

基本上,它以数组为参数。根据某些情况,它会将元素添加到数组中。我想知道这是否有效?如果像所有数组一样通过引用传递,那么我认为它是有效的。但是,如果它只是被复制并按值传递,那么它就不是了。$arout

那么判决是什么呢?

PHP 数组 按引用传递

答:

1赞 reformed 8/23/2016 #1

根据手册,PHP 数组是按值传递的:

数组赋值始终涉及值复制。使用 reference 运算符按引用复制数组。

如果要传递数组的引用,请使用上面提到的相应运算符 (&),并删除函数中的行:return $arout;addWeight()

<?php
// pass $array by reference using & operator
addWeight(&$array, $lineCountry, $lineDomain, $target, $weight);