473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to pass array names to function parameters ?


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
Apr 15 '06 #1
10 15418
On Sat, 2006-04-15 at 12:26 +0000, to**@tony.com wrote:
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


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
Apr 15 '06 #2
Actually, you cannot use superglobal arrays as variable variables:
From PHP manual:

http://www.php.net/manual/en/languag...predefined.php

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

Apr 15 '06 #3
In article <11**********************@v46g2000cwv.googlegroups .com>,
da****@gmail.com says...
Actually, you cannot use superglobal arrays as variable variables:
From PHP manual:

http://www.php.net/manual/en/languag...predefined.php

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


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
Apr 15 '06 #4
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.

Apr 15 '06 #5
In article <11**********************@j33g2000cwa.googlegroups .com>,
da****@gmail.com says...
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.


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
Apr 16 '06 #6
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

Apr 16 '06 #7
In article <11**********************@i40g2000cwc.googlegroups .com>,
da****@gmail.com says...
I knew that I forgot something :/


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
Apr 17 '06 #8

to**@tony.com wrote:
In article <11**********************@i40g2000cwc.googlegroups .com>,
da****@gmail.com says...
I knew that I forgot something :/


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 ;-)


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

Apr 17 '06 #9
In article <11**********************@g10g2000cwb.googlegroups .com>,
ke******@gmail.com says...
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


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

Apr 18 '06 #10
to**@tony.com schrieb:
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


Try this one:

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

Apr 18 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Miguel | last post by:
Hello. I have a Javascript function that validates an specific form. As parameters this function receives an array of elements to be checked. Depending on the form in cause, the array could have...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
9
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common...
3
by: questions? | last post by:
I tried to pass a two dimensional array in the function arguments the following program is a demonstration, ******************************************** # include <stdio.h> # include...
10
by: tshad | last post by:
I want to access multiple arguments based on name passed. For example I have the following asp:textboxes: BillingAddress1 BillingAddress2 BillingCity ShippingAddress1 ShippingAddress2...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.