PHP: Array to variable function parameter values

后端 未结 2 729
走了就别回头了
走了就别回头了 2021-02-19 07:08

I have a variable length array that I need to transpose into a list of parameters for a function.

I hope there is a neat way of doing this - but I cannot see how.

<
2条回答
  •  失恋的感觉
    2021-02-19 07:29

    Yes, use call_user_func_array():

    call_user_func_array('function_name', $parameters_array);
    

    You can use this to call methods in a class too:

    class A {
      public function doStuff($a, $b) {
        echo "[$a,$b]\n";
      }
    }
    
    $a = new A;
    call_user_func_array(array($a, 'doStuff'), array(23, 34));
    

提交回复
热议问题