473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing variable number of arguments from array to function with call_user_func()?

rob
Hello,

If I have an array made up of a bunch of key =value pairs, how can I
pass the values of each key as an argument to a function, given that
the number of items in the array are not static (i.e: sometimes
there's one item, sometimes there's two)?

For example, if I have the following array:

$list = array('sky' ='blue', 'grass' ='green');
$list = array_values($list);

I'd like to pass the two values in $list to a function that has two
arguments, i.e:

function color($sky, $grass) { }

The trick is is that $list will not always contain two items -
sometimes one, three, four, etc, so it has to pass them dynamically to
the function. I know about call_user_func(), and how I can pass
parameters to it, but I'm having a difficult time trying to pass
multiple parameters given that I don't know how many I need to pass.

Can anyone help?

Rob

Aug 13 '07 #1
3 3412

"rob" <ro*@rawb.netschreef in bericht
news:11*********************@q75g2000hsh.googlegro ups.com...
Hello,
<snip>
The trick is is that $list will not always contain two items -
sometimes one, three, four, etc, so it has to pass them dynamically to
the function. I know about call_user_func(), and how I can pass
parameters to it, but I'm having a difficult time trying to pass
multiple parameters given that I don't know how many I need to pass.

Can anyone help?

Rob
call_user_func_array()
Aug 13 '07 #2
rob wrote:
Hello,

If I have an array made up of a bunch of key =value pairs, how can I
pass the values of each key as an argument to a function, given that
the number of items in the array are not static (i.e: sometimes
there's one item, sometimes there's two)?

For example, if I have the following array:

$list = array('sky' ='blue', 'grass' ='green');
$list = array_values($list);

I'd like to pass the two values in $list to a function that has two
arguments, i.e:

function color($sky, $grass) { }

The trick is is that $list will not always contain two items -
sometimes one, three, four, etc, so it has to pass them dynamically to
the function. I know about call_user_func(), and how I can pass
parameters to it, but I'm having a difficult time trying to pass
multiple parameters given that I don't know how many I need to pass.

Can anyone help?

Rob
Just pass them as an array, i.e.

color($list);

function color ($colors) {
if (is_array($colors))
foreach ($colors as $c)
process one color from the list here
else
process a single color here
}

You might want to make this a function which pulls out each item in the
list and calls another function to process a single color (if the
processing is separate, that is).


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 13 '07 #3
C.
On 13 Aug, 01:42, rob <r...@rawb.netwrote:
>
For example, if I have the following array:

$list = array('sky' ='blue', 'grass' ='green');
$list = array_values($list);

I'd like to pass the two values in $list to a function that has two
arguments, i.e:

function color($sky, $grass) { }
call_user_func('color', $list);

function color()
{
$params = func_get_args();
....

But it's much cleaner to do this:

color($list);

function color($list) // expect an array as input
{
....

Which eliminates the positional problems which are exacerbated by
variable argument parsing.

C.
Aug 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
12
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
2
by: Mary Pegg | last post by:
No point re-inventing the wheel, so thought I'd see if anybody's got some good example code kicking around. (Please excuse trivial syntactical errors, it's all coming off the top of my head). ...
6
by: virgincita schmidtmann | last post by:
Good evening, I would like to pass the size of an array from the commandline. int main(int argc, int *argv) { .... max=*argv; int list; ....
5
by: rafal.m | last post by:
Hi This code fails: class K2 { public function increment(&$obj) { $obj += 1; }
9
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.