RE: expand array into function arguments?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Christopher J. Bottaro [mailto:cjbottaro@xxxxxxxxxxxxxxxxxxxx]
> Sent: Thursday, 12 May 2005 5:13 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  expand array into function arguments?
> 
> You can do this in Python:
> 
> <code>
> def myFunc(arg1, arg2, arg):
>   #do something
> 
> myList = [1, "arg", 5]
> myFunc(*myList) # calls myFunc(1, "arg", 2)
> <code>
> 
> Can that be done in PHP, and if so, how?

Don't know if there's a better solution, but at the very least you could put
the values in an array, and pass the array to the function.

$arr = array();
$arr[] = "First";
$arr[] = "Second";
$arr[] = "Third";

$val = my_function($arr);

function my_function($arr_in){
	// do something with $arr_in[0];

	// do something with $arr_in[1];

	// do something with $arr_in[2];
}

Note: if you wanted the function to be able to change the actual values in
the array, you would need to pass it by reference rather than by value, eg:

Function my_function(&$arr_in){ etc...

The ampersand before the variable name in the function declaration indicates
the variable is being passed by reference.

Just a thought.

Much warmth,

Murray

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux