gpt4 book ai didi

PHP从函数传递数组变量

转载 作者:行者123 更新时间:2023-12-02 04:44:08 25 4
gpt4 key购买 nike

我尝试从函数处理全局数组。这是我的代码:

$myArray = array();
myfunc($myArray);
var_dump($myArray);

function myfunc($myArray){
//perform some other tasks
$myArray['name']='John';
}

但是没有用。 var_dump 返回空数组。我怎样才能让数组传递到全局?

谢谢。

最佳答案

您需要将 is 作为 reference 传递&

尝试这样做:

function myFunc(&$myArray){
//perform some other tasks
$myArray['name']='John';
}

您也可以这样返回:

$myArray = array();
$myArray = myfunc($myArray);
var_dump($myArray);

function myfunc($myArray){
//perform some other tasks
$myArray['name']='John';
return $myArray;
}

关于PHP从函数传递数组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20171102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com