glenelkins Posted June 5, 2009 Share Posted June 5, 2009 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/ Share on other sites More sharing options...
GingerRobot Posted June 5, 2009 Share Posted June 5, 2009 person($args[0],$args[1],$args[2]) ? Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849815 Share on other sites More sharing options...
glenelkins Posted June 5, 2009 Author Share Posted June 5, 2009 there will be an unknown number of items in the array! Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849816 Share on other sites More sharing options...
Daniel0 Posted June 5, 2009 Share Posted June 5, 2009 call_user_func_array Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849817 Share on other sites More sharing options...
glenelkins Posted June 5, 2009 Author Share Posted June 5, 2009 can this function be used to call a method within a class?? $object->function() Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849861 Share on other sites More sharing options...
Daniel0 Posted June 5, 2009 Share Posted June 5, 2009 Yeah call_user_func_array(array($object, 'function'), array('arg1', 'arg2')); Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849869 Share on other sites More sharing options...
Mark Baker Posted June 5, 2009 Share Posted June 5, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/161031-send-array-as-function-arguments/#findComment-849905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.