Re: [PHP] Self generating variables/arrays




On Nov 26, 2006, at 5:11 AM, Stut wrote:

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
Thanks for the reply;
The problem is
1. one large array exploded around \n
2. then a loop looks for '<br><br>\n' items
and records the index number of these items
as well of keeping a count: ($c)
3. Since $c is a variable it can represent a different
number each time.
4. Since I don't know what $c is going to be I can't
declare array variables with predetermined names
5. I want to slice the large array into $c arrays that
begin and end around items in the large array
containing '<br><br>\n';
This is as much abstraction as I can give you
In context it is represented by several hundred lines of function code.
This code is supposed to format the result of a search operation.
It is tailored for a custom application. I don't want to rewrite the
whole ball of wax just so I can use someone's wonderful pre
packaged code library or applications.
(I have gone ahead with experimenting with this code and
was successful, up to a point...)
Thanks again;
JK


.



Relevant Pages

  • Re: Is it possible to use the value of the PROGRAM ID within the source code?
    ... Until the universal availability of scope terminators there were ... But 'reusability' is not a unique feature of OO design. ... remain and some are now garbage. ... A tree can be made faster by increasing its width; an array ...
    (comp.lang.cobol)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: Postscript questions
    ... It looks as if you believe the curly braces are defining a scope. ... Curly braces are behaving much more like the square brackets, the array "delimiters". ... The difference is deferred execution, ... Evaluating an executable name means that the interpreter searchs the name through the dictionnary stack and when found, evaluate the value associated with the name. ...
    (comp.lang.postscript)
  • Re: [crit] Fallen
    ... I personally would class an array as a persistent data structure, ... assuming that it doesn't have local scope. ... A persistent array requires making a copy of the whole array each time ... impose new terminology by getting the old terminology wrong, ...
    (rec.arts.sf.composition)
  • Re: Postscript questions
    ... It looks as if you believe the curly braces are defining a scope ... The difference is deferred execution (and ... A procedure is an executable array. ... Evaluating an executable name means that the interpreter searchs ...
    (comp.lang.postscript)