Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个函数之间的PHP传递数组_Php_Arrays_Function - Fatal编程技术网

两个函数之间的PHP传递数组

两个函数之间的PHP传递数组,php,arrays,function,Php,Arrays,Function,我目前有2项职能;我需要从另一个函数的内部读取其中一个函数中的一个数组: function a(){ foreach ($options as $option){ // $options is a variable from function b() // here goes a really long loop } } function b(){ $options[] = array( 'key1' => 1, 'key2' => 2, 'key

我目前有2项职能;我需要从另一个函数的内部读取其中一个函数中的一个数组:

function a(){

 foreach ($options as $option){ // $options is a variable from function b()
  // here goes a really long loop
 }

}

function b(){

 $options[] = array(
   'key1' => 1,
   'key2' => 2,
   'key3' => 3,
  );

 function a(); // run function here?
}
我的php文件将包含我们称之为“函数b()”的多个副本。在每个函数b()中,我想运行函数a(),但使用函数b()中的数组内容

函数a()包含一个始终相同但非常长的循环,我只想让代码尽可能短,而不是在每个函数b()中复制函数a()


这可能真的很容易,但我已经为这个问题挣扎了很长时间了

您不能简单地将其作为函数参数传递吗

function a($options){
    foreach ($options as $option){ // $options is a variable from function b()
        // here goes a really long loop
    }
}

function b(){
    a(array(
        'key1' => 1,
        'key2' => 2,
        'key3' => 3,
    ));
}

您能不能不简单地将其作为函数参数传递

function a($options){
    foreach ($options as $option){ // $options is a variable from function b()
        // here goes a really long loop
    }
}

function b(){
    a(array(
        'key1' => 1,
        'key2' => 2,
        'key3' => 3,
    ));
}

只需从
b()
返回数组,然后将其作为参数传递给
a()


只需从
b()
返回数组,然后将其作为参数传递给
a()


忘了提到我必须在函数b()中运行函数a()。在我的场景中,函数b()是网页上的一个选项卡。找到另一个解决方案了吗?更新了我的答案,但忘了提到我必须在函数b()中运行函数a()。在我的场景中,函数b()是网页上的一个选项卡。有其他解决方案吗?相应地更新了我的答案