Passing variable number of arguments from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
philentropist
Forum Newbie
Posts: 9
Joined: Wed Jan 16, 2008 7:59 pm

Passing variable number of arguments from array

Post by philentropist »

Given an array, is it possible to pass its elements as arguments to a function (such as sprintf()) that takes a variable number of arguments?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Passing variable number of arguments from array

Post by s.dot »

Yes. func_get_args() and func_num_args() may be useful for you.

EDIT| You'd have to make your own custom function for this. If you simply want to apply a function to every member of an array, use array_map().
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Passing variable number of arguments from array

Post by jimthunderbird »

Sure, you can pass the reference of the array to a function. This is a basic programming fact.
philentropist
Forum Newbie
Posts: 9
Joined: Wed Jan 16, 2008 7:59 pm

Re: Passing variable number of arguments from array

Post by philentropist »

Let me be more specific about what I need.

The sprintf function takes a variable number of arguments as follows:
sprintf("%d %s cats", 3, "crazy");
The above code outputs "3 crazy cats".

I want to define a function with a variable number of arguments (or one that takes an array), do some processing on the array elements, and pass them to sprintf. The question is how to pass them to sprintf. A reference to the array does not work (sorry Jim, but when you're being condescending you should at least be correct, that's a basic life fact).

For example, can you define a function of an array printPlusOne() that behaves as follows (using sprintf internally)?
printPlusOne("%d", 5); // outputs "6"
printPlusOne("%d %d %d", 1, 2, 3); // ouptuts "2 3 4"
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Passing variable number of arguments from array

Post by Zoxive »

From my understanding of the OP, I believe they mean call a function with the Array of Arguments.

Example.

Code: Select all

$Array = array('Hello %s, It is %s','Name', date('m-d-Y'));
 
echo call_user_func_array('sprintf',$Array);
// Outputs
// Hello Name, It is 01-16-2008
 
function my_sprintf(array $Data){
  return call_user_func_array('sprintf',$Data);
}
echo my_sprintf($Array);
// Outputs
// Hello Name, It is 01-16-2008
philentropist
Forum Newbie
Posts: 9
Joined: Wed Jan 16, 2008 7:59 pm

Re: Passing variable number of arguments from array

Post by philentropist »

Exactly what I needed. Thanks Zoxive!
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Passing variable number of arguments from array

Post by jimthunderbird »

+1 for zoxive :D
Post Reply