PHP: Array to variable function parameter values

后端 未结 2 730
走了就别回头了
走了就别回头了 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:28

    You could use eval:

      eval("$func_name(" . implode($params, ",") . ");");
    

    Though you might need to do some lambda trickery to get your parameters quoted and/or escaped:

      $quoted_escaped_params = array_map(create_function('$a', 'return "\"". str_replace("\"",` "\\\"", $a) . "\""'), $params);
    

提交回复
热议问题