PDA

View Full Version : array to function


MattF
10-16-2000, 07:40 AM
Hi,

I'm using PHP 4.0.2 and Apache, standard
set-up.

How can I pass an array as an argument to a
function, it seems you can only pass variables and
constants. Is there any way to do?

Shalazar
10-16-2000, 03:50 PM
You should be able to pass an array just fine to a function.[nbsp][nbsp]To exemplify you can pass an array, I just wrote this little code that accepts an array as input, and will tell you it is infact an array, and then read through it to print out each element of the $testarray passed to it:


<?php

function readArray ( $foo_array )
{
[nbsp][nbsp]print &quot;The argument passed is of type:&quot; .gettype($foo_array). &quot;
&quot;;
[nbsp][nbsp]foreach ($foo_array as $elements) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]print &quot;$elements
&quot;;
[nbsp][nbsp] }
}

$testarray = array ( &quot;John&quot; , &quot;Kim&quot; , &quot;Bob&quot; );
readArray ($testarray);

?>

MattF
10-17-2000, 07:06 AM
More specific try passing $HTTP_POST_VARS to the function, this is what I'm trying to do. I can't understand why it works for a user created array and not for $HTTP_POST_VARS. The function I'm trying to write process $HTTP_POST_VARS, but I've had no success in passing $HTTP_POST_VARS to the function.[nbsp]

Shalazar
10-17-2000, 02:24 PM
Here's something simple that will print out the contents of the $HTTP_POST_VARS array:


<?php

foreach ($HTTP_POST_VARS as $key=>$value)
{
[nbsp][nbsp]if ( gettype ( $value ) == &quot;array&quot;)[nbsp][nbsp]
[nbsp][nbsp]{
[nbsp][nbsp][nbsp][nbsp]print &quot;$key ==
\n&quot;;
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] foreach ( $value as $elements ) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]print &quot;........$elements
&quot;;
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] }
[nbsp][nbsp] }

[nbsp][nbsp] else {
[nbsp][nbsp][nbsp][nbsp] print &quot;$key == $value
\n&quot;;
[nbsp][nbsp] }

}
?>


Just be sure you have some user input around to feed into it.[nbsp][nbsp]Otherwise, it will error out about line 3, because no $HTTP_POST_VARS will exist.
[This message has been edited by Shalazar (edited 10-17-00@1:26 pm)]