arrays as function arguments

arrays as function arguments

am 09.09.2007 20:56:20 von Sam Waller

I'm trying do do something like this:

$a = array();
func($a);
printf("array size in main = %d\r\n", count($a));

/////////////////////////////////////


function func($_a) {
array_push($_a, 22);
printf("array size in func = %d\r\n", count($_a));
}


This prints:
array size in main = 0
array size in func = 1

It looks like the data is no longer available in the calling function. Is there a way to get this to
work so that I have the computed array available in the calling function?

thanks,
Sam

Re: arrays as function arguments

am 09.09.2007 21:15:21 von Jerry Stuckle

Sam Waller wrote:
> I'm trying do do something like this:
>
> $a = array();
> func($a);
> printf("array size in main = %d\r\n", count($a));
>
> /////////////////////////////////////
>
>
> function func($_a) {
> array_push($_a, 22);
> printf("array size in func = %d\r\n", count($_a));
> }
>
>
> This prints:
> array size in main = 0
> array size in func = 1
>
> It looks like the data is no longer available in the calling function. Is there a way to get this to
> work so that I have the computed array available in the calling function?
>
> thanks,
> Sam
>

Your output doesn't make sense. If you were calling func() before the
printf in in your calling routine, your messages would be in the
opposite order.

To answer your question, check out
http://us2.php.net/manual/en/language.references.php.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: arrays as function arguments

am 09.09.2007 23:01:13 von Henk Verhoeven

Sam Waller wrote:
> I'm trying do do something like this:
>
> $a = array();
> func($a);
> printf("array size in main = %d\r\n", count($a));
>
> /////////////////////////////////////
>
>
> function func($_a) {
> array_push($_a, 22);
> printf("array size in func = %d\r\n", count($_a));
> }
>
>
> This prints:
> array size in main = 0
> array size in func = 1
>
> It looks like the data is no longer available in the calling function. Is there a way to get this to
> work so that I have the computed array available in the calling function?

function func(&$_a) {

Greetings,

Henk.