Jump to content

Send Array As Function Arguments


glenelkins

Recommended Posts

Hi

 

Lets say i have an array like this $args = array ( 'Hi', '24', 'Mark' );

 

and a function:

 

function person ( $msg, $age, $name ) {

}

 

i want to be able to call the function person() parsing the correct parameters from the array, so it would work something like person ( 'Hi', '24', 'Mark' );  not person ( $args );

Link to comment
https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/
Share on other sites

You will need to modify your function to allow for a variable number of arguments as well:

function person ( $msg, $age, $name ) {
}

demands 3 arguments, so if the array you're using only has 2 entries, you'll get an error

 

 

Use defaults in the function definition:

function person ( $msg='Hi', $age=32, $name='Mark' ) {
}

 

 

Alternatively

function person () {
   $args = func_get_args();
}

or

function person ($args=array()) {
   foreach($args as $key => $value) {
      $$key = $value;
   }
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.