drgawfsf1069 2015-11-09 16:10
浏览 23
已采纳

从Array添加现有PHP函数的参数

I'm looking to figure out how to add more arguments to a function call based on an Array and can't figure out what I need to do. The function in question doesn't work with simply passing an array, but the array has to explode somehow as if each array element was a new argument in the function call. I don't think call_user_func_array works but perhaps I don't know how to execute it properly. To give some context, the array of arguments are coming from a varying amount of $_GET arguments which is processed by an API class file I have no control over, but it appears that I can add a lot of arguments that allow the results to filter.

$arrayofarguments = array("dynamicarg1","dynamicarg2","dynamicarg3");

$runthis = example($staticargument1,
                 $staticargument2,
                 $staticargument3,
                 $arrayofarguments,
                 $staticargument4); 

//run the results
echo $runthis;

Expected result

$runthis = example($staticargument1,
                 $staticargument2,
                 $staticargument3,
                 "dynamicarg1",
                 "dynamicarg2",
                 "dynamicarg3",
                 $staticargument4);

Thanks for the help! :)

  • 写回答

1条回答 默认 最新

  • duanliao6789 2015-11-09 16:15
    关注

    I think you have to do something like

    function example("blaa","foo","bar",$arrayofarguments,"test") {
    
      $args = func_get_args();
    
      // $arg[3] would be $arrayofarguments
      foreach ( $arg[3] as $function) {
        $function(); // this calls the function
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog