3

I was thinking about the possibility of accessing all the variables that are passed into an function, and merge them into an array. (Without passing variables into an array from the beginning)

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 Answers 3

9

http://php.net/func_get_args

Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe how easy that was. Thanks a lot for one of the fastest answers on SO I have gotten this far :)
9

Take a look at func_get_args function. This is how you can build an array for them:

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);
}

Comments

1

It can be useful to clearly identify that the items you are passing in should be treated as an array. For instance, set your function def to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.