Connecting Tech Pros Worldwide Forums | Help | Site Map

How to pass array names to function parameters ?

tony@tony.com
Guest
 
Posts: n/a
#1: Apr 15 '06

i'm trying to itterate through an array that contains the names of the
global arrays

eg:

$myarray = array("\$_GET", "\$_SERVER"); and so on

The problem Im having is calling a function with

myfunction($myarray[0]);

doesnt seem to pass the array name in a manner I can then use.

I'm sure I'm missing something simple here can anyonehelp please ?

tony

Scott
Guest
 
Posts: n/a
#2: Apr 15 '06

re: How to pass array names to function parameters ?


On Sat, 2006-04-15 at 12:26 +0000, tony@tony.com wrote:[color=blue]
> i'm trying to itterate through an array that contains the names of the
> global arrays
>
> eg:
>
> $myarray = array("\$_GET", "\$_SERVER"); and so on
>
> The problem Im having is calling a function with
>
> myfunction($myarray[0]);
>
> doesnt seem to pass the array name in a manner I can then use.
>
> I'm sure I'm missing something simple here can anyonehelp please ?
>
> tony[/color]

That should pass the name of the global array. You could also use single
quotes and drop the escape character:

$myarray = array('$_GET, '$_SERVER');

Are you sure the problem isn't in your function? What are you doing
with the name once your function receives it?

Scott


damirz@gmail.com
Guest
 
Posts: n/a
#3: Apr 15 '06

re: How to pass array names to function parameters ?


Actually, you cannot use superglobal arrays as variable variables:
[color=blue]
>From PHP manual:[/color]
http://www.php.net/manual/en/languag...predefined.php

"Variable variables: Superglobals cannot be used as variable variables
inside functions or class methods."

tony@tony.com
Guest
 
Posts: n/a
#4: Apr 15 '06

re: How to pass array names to function parameters ?


In article <1145117876.799548.249800@v46g2000cwv.googlegroups .com>,
damirz@gmail.com says...[color=blue]
> Actually, you cannot use superglobal arrays as variable variables:
>[color=green]
> >From PHP manual:[/color]
> http://www.php.net/manual/en/languag...predefined.php
>
> "Variable variables: Superglobals cannot be used as variable variables
> inside functions or class methods."
>
>[/color]

Oh bugger.

Thanks for that.

I was just trying to get all the superglobals to echo out to the browser
without having to code every one seperately...


something like:

function showthisarray($nextGlobal) {
echo($nextGlobal . "\n"); // debug line
foreach($nextGlobal as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

The idea being to just pass the array names in sequence or as selected.

The function does get the name of the relevant array as the first echo
prints it ok eg . $_GET or whatever as is passed into $nextGlobal

But the foreach() function just generates a bad parameter error.

I guess it will have to be the long winded solution.

tony
Sidonath
Guest
 
Posts: n/a
#5: Apr 15 '06

re: How to pass array names to function parameters ?


But you still can try with $HTTP_*_VARS :)

// $nextGlobal = {"GET", "POST", ...}

function showthisarray($nextGlobal) {
echo($nextGlobal . "\n"); // debug line
foreach(${'HTTP_' . $nextGlobal . '_VARS'} as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

I didn't write much PHP lately, so sorry if there is some syntax (or
any other) error here.

tony@tony.com
Guest
 
Posts: n/a
#6: Apr 16 '06

re: How to pass array names to function parameters ?


In article <1145134642.131789.187380@j33g2000cwa.googlegroups .com>,
damirz@gmail.com says...[color=blue]
> But you still can try with $HTTP_*_VARS :)
>
> // $nextGlobal = {"GET", "POST", ...}
>
> function showthisarray($nextGlobal) {
> echo($nextGlobal . "\n"); // debug line
> foreach(${'HTTP_' . $nextGlobal . '_VARS'} as $k => $v ) {
> echo($k . " = " . $v . "\n");
> }
> }
>
> I didn't write much PHP lately, so sorry if there is some syntax (or
> any other) error here.
>
>[/color]

Worth a try but it produces the same result. It seems you can't pass the
array name in a string at all from what I can see.

tony


Sidonath
Guest
 
Posts: n/a
#7: Apr 16 '06

re: How to pass array names to function parameters ?


I knew that I forgot something :/

$HTTP_*_VARS arrays are not superglobal. Hence you need to specify in
function that you're using global arrays or access them with $GLOBALS
array

Here's a bit modified example that should work:

------------------------

// custom global array
$myArray = array("some", "elements");

// array with names of global arrays you want to access
$globalArrayNames = array("HTTP_GET_VARS", "HTTP_POST_VARS",
"myArray");

// parameter is global array name
function showThisArray($globalArrayName) {
echo($globalArrayName . "\n"); // debug line
foreach($GLOBALS[$globalArrayName] as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

showThisArray($globalArrayNames[0]);
showThisArray($globalArrayNames[1]);
showThisArray($globalArrayNames[2]);

------------------------

Hope this helps

tony@tony.com
Guest
 
Posts: n/a
#8: Apr 17 '06

re: How to pass array names to function parameters ?


In article <1145189729.880217.112930@i40g2000cwc.googlegroups .com>,
damirz@gmail.com says...[color=blue]
> I knew that I forgot something :/
>[/color]

Nice one. Modified slightly & tested - it works fine - as below.
I added SERVER just to be sure of some output from a Sglobal
So much nicer than having to do each one at a time ;-)

<?php
// custom global array
$myArray = array("some", "elements");

// array with names of global arrays you want to access
$globalArrayNames = array("HTTP_SERVER_VARS", "HTTP_POST_VARS",
"myArray");

// parameter is global array name
function showThisArray($globalArrayName) {
echo("<br><br>" . "[ " . $globalArrayName . " ]<br>");
foreach($GLOBALS[$globalArrayName] as $k => $v ) {
echo($k . " = " . $v . "<br>");
}
}

showThisArray($globalArrayNames[0]);
showThisArray($globalArrayNames[1]);
showThisArray($globalArrayNames[2]);
?>

tony
Ken Robinson
Guest
 
Posts: n/a
#9: Apr 17 '06

re: How to pass array names to function parameters ?



tony@tony.com wrote:[color=blue]
> In article <1145189729.880217.112930@i40g2000cwc.googlegroups .com>,
> damirz@gmail.com says...[color=green]
> > I knew that I forgot something :/
> >[/color]
>
> Nice one. Modified slightly & tested - it works fine - as below.
> I added SERVER just to be sure of some output from a Sglobal
> So much nicer than having to do each one at a time ;-)
>[/color]

Here's another way of doing this.

<?php
// custom global array
$myArray = array("some", "elements");

// array with names of global arrays you want to access
$globalArrayNames =
array(array('$_SERVER',$_SERVER),array('$_POST',$_ POST),
array("myArray",$myArray));

// parameter is global array name
function showThisArray($globalArrayName) {
echo("<br><br>" . "[ " . $globalArrayName[0] . " ]<br>");
echo '<pre>' . print_r($globalArrayName[1],true) . '</pre>';
}

showThisArray($globalArrayNames[0]);
showThisArray($globalArrayNames[1]);
showThisArray($globalArrayNames[2]);
?>


Ken

tony@tony.com
Guest
 
Posts: n/a
#10: Apr 18 '06

re: How to pass array names to function parameters ?


In article <1145310837.226085.226960@g10g2000cwb.googlegroups .com>,
kenrbnsn@gmail.com says...
[color=blue]
> Here's another way of doing this.
>
> <?php
> // custom global array
> $myArray = array("some", "elements");
>
> // array with names of global arrays you want to access
> $globalArrayNames =
> array(array('$_SERVER',$_SERVER),array('$_POST',$_ POST),
> array("myArray",$myArray));
>
> // parameter is global array name
> function showThisArray($globalArrayName) {
> echo("<br><br>" . "[ " . $globalArrayName[0] . " ]<br>");
> echo '<pre>' . print_r($globalArrayName[1],true) . '</pre>';
> }
>
> showThisArray($globalArrayNames[0]);
> showThisArray($globalArrayNames[1]);
> showThisArray($globalArrayNames[2]);
> ?>
>
>
> Ken
>
>[/color]

And there was me thinking it couldnt be done ... ;-)
Now we have two ways.

Correct me if I'm wrong though Ken but doesnt this method pass the actual
array rather than just the name?

I'm not sure because I'm not able to work out what this is doing exactly
(I'm new to PHP as you may have gathered)

tony

milahu
Guest
 
Posts: n/a
#11: Apr 18 '06

re: How to pass array names to function parameters ?


tony@tony.com schrieb:
[color=blue]
> i'm trying to itterate through an array that contains the names of the
> global arrays
>
> eg:
>
> $myarray = array("\$_GET", "\$_SERVER"); and so on
>
> The problem Im having is calling a function with
>
> myfunction($myarray[0]);
>
> doesnt seem to pass the array name in a manner I can then use.
>
> I'm sure I'm missing something simple here can anyonehelp please ?
>
> tony[/color]

Try this one:

$arrs = array('_GET', '_POST');
foreach($arrs as $arrnam)
print_r($$arrnam);

Closed Thread