PHP: Collect all the variables passed to the function as an array?

I thought about the possibility of accessing all the variables that are passed to the function and combining them into an array. (Without passing variables to the array from the start)

Pseudo Code:

// 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
    }
}
+3
source share
3 answers

Take a look at . Here's how you can build an array for them: 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);
}
+9
source

, , , . , def

function newFunction( $arrayOfArguments ) { 
    foreach($arrayOfArguments as $argKey => $argVal)
    { /* blah */ }
}
+1

Source: https://habr.com/ru/post/1748061/


All Articles