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
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源