在php中使用函数向数组中添加项不起作用

在php中使用函数向数组中添加项不起作用,php,arrays,Php,Arrays,我已经创建了一个关联数组 $prijs = array ( "Black & Decker Accuboormachine" => 148.85, "Bosch Boorhamer" => 103.97, "Makita Accuschroefmachine" => 199.20, "Makita Klopboormachine" => 76.00, "Metabo Klopboor" => 119.00

我已经创建了一个关联数组

$prijs = array (
    "Black & Decker Accuboormachine" => 148.85,
    "Bosch Boorhamer" => 103.97,
    "Makita Accuschroefmachine" => 199.20,
    "Makita Klopboormachine" => 76.00,
    "Metabo Klopboor" => 119.00        
);
现在我必须向数组中添加一个值,我想使用一个函数来实现这一点

function itemToevoegen($array, $key, $value){
    $array[$key] = $value;
}
然后我调用函数:

itemToevoegen($prijs, "Bosch GBH 18 V-LI", 412.37);
我在没有将数组名放入输入参数的情况下尝试了这一点,但这也不起作用

============================编辑===================== 当我输入这个时,我想我必须返回值,但这也不能给我想要的结果

function itemToevoegen($array, $key, $value){
    return $array[$key] = $value;
}
谁能帮我一下,告诉我这里缺少什么

提前谢谢


通过引用传递意味着您可以修改调用者看到的变量。为此,请在函数定义中的参数名称前加一个符号AND

默认情况下,函数参数是按值传递的,因此,如果函数内参数的值发生更改,则不会在函数外进行更改

有两种选择:

可以通过引用将变量传递给函数,以便函数可以修改变量

或者返回
数组
并设置新值

function itemToevoegen($array, $key, $value){
    $array[$key] = $value;
    return $array;
}

$prijs = itemToevoegen($prijs, "Bosch GBH 18 V-LI", 412.37);

感谢您的快速回复@Leggendario。我添加了&它就像一个符咒。您能解释一下这在上下文中的作用吗?通过引用传递意味着您可以修改调用者看到的变量。为此,请在函数定义中的参数名称前加一个与号。谢谢您的回答。
function itemToevoegen(&$array, $key, $value){
    $array[$key] = $value;
}
function itemToevoegen($array, $key, $value){
    $array[$key] = $value;
    return $array;
}

$prijs = itemToevoegen($prijs, "Bosch GBH 18 V-LI", 412.37);