PHP:收集作为数组传递给函数的所有变量?

Ind*_*ial 3 php arrays function

我正在考虑访问传递给函数的所有变量的可能性,并将它们合并到一个数组中.(不从一开始就将变量传递给数组)

伪代码:

// Call function
newFunction('one', 'two', 'three' ) ;// All values are interpreted as a one rray in some way

// Function layout
newFunction( ) {    
   // $functionvariables =  array( All passed variables) 

    foreach ($functionvariable as $k => $v) {
        // Do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

Sar*_*raz 9

看看func_get_args功能.这是你为它们构建数组的方法:

function test()
{
  $numargs = func_num_args();

  $arg_list = func_get_args();
  $args = array();

  for ($i = 0; $i < $numargs; $i++)
  {
    $args[] = $arg_list[$i];
  }

  print_r($args);
}
Run Code Online (Sandbox Code Playgroud)