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

This is a discussion on Passing variable number of arguments from array to function with call_user_func()? within the PHP Language forums, part of the PHP Programming Forums category; Hello, If I have an array made up of a bunch of key => value pairs, how can I pass ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2007
rob
 
Posts: n/a
Default Passing variable number of arguments from array to function with call_user_func()?

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

Reply With Quote
  #2 (permalink)  
Old 08-13-2007
amygdala
 
Posts: n/a
Default Re: Passing variable number of arguments from array to function with call_user_func()?


"rob" <rob@rawb.net> schreef in bericht
news:1186965770.780865.46300@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()


Reply With Quote
  #3 (permalink)  
Old 08-13-2007
Jerry Stuckle
 
Posts: n/a
Default Re: Passing variable number of arguments from array to function withcall_user_func()?

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.
jstucklex@attglobal.net
==================
Reply With Quote
  #4 (permalink)  
Old 08-13-2007
C.
 
Posts: n/a
Default Re: Passing variable number of arguments from array to function with call_user_func()?

On 13 Aug, 01:42, rob <r...@rawb.net> wrote:
>
> 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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:13 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0