Re: Self generating variables/arrays

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

 



jekillen wrote:
I am writing some code that will format results of a search for display.
I need to split an array into several different arrays but I won't know
before hand how many, so, I am looking for a way to dynamically
generate arrays for this purpose.
My present direction is to use the following code:

for($i = 0; $i < $c; $i++)
    { eval('$a_'.$i.' = array();'); }

Where '$c' is the derived number of arrays need to hold the
pieces of the bigger array. My confusion, though, is; since
these are created in the scope of the for loop, I don't know
if I can use them elsewhere in the code. The Global statement
sends them outside the scope of the function this code is in,
or does it? And I'm not even sure I am on the right track.
Perhaps someone can say yay or nay on the spot, if not
I can go back and do some experimenting.

I'm not sure why you think you need to do this. In PHP you do not need to 'declare' variables, you can just use them. So unless you're checking for the existance of these arrays after this loop, the loop is completely pointless. But you haven't included enough information about how you intend to use these arrays to give a definite answer.

As far as scope goes, if you want to create these variables in the global scope, create them as elements of the $GLOBALS array [http://php.net/manual/en/reserved.variables.php#reserved.variables.globals].

-Stut

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