473,800 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($l ist);

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 3447

"rob" <ro*@rawb.netsc hreef in bericht
news:11******** *************@q 75g2000hsh.goog legroups.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($l ist);

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($colo rs))
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*******@attgl obal.net
=============== ===
Aug 13 '07 #3
C.
On 13 Aug, 01:42, rob <r...@rawb.netw rote:
>
For example, if I have the following array:

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

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
14956
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10188
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 code... TCHAR myArray; DoStuff(myArray);
17
3606
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
12
6908
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 Array Variable values are calculated in the function. Calling procedure receives the values preferably into variables of the same
1
3270
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 irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. This routine would be called quite simply as follows: void main() {
2
4623
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). Say I have a function thus: function munge($a) { return ($a * 2);
6
6935
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
4189
by: rafal.m | last post by:
Hi This code fails: class K2 { public function increment(&$obj) { $obj += 1; }
9
5286
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 defined and depends on a function property named arguments to process its input. I poked around and found this is deprecated. How do you pass an unknown number of arguments to a function? Put them in an array?
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.