This is a discussion on using [] inside eval function within the PHP Language forums, part of the PHP Programming Forums category; Hi All, I'm using eval and arrays in foreach and have trouble with adding elements to them - I'm ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi All,
I'm using eval and arrays in foreach and have trouble with adding elements to them - I'm talking about the '[]' operator. My code is: // creates arrays with the names of columns in keys array foreach ($keys as $k) { eval("\$$k = array();"); } // fills dynamically columns' arrays with its values foreach ($array as $a) { foreach ($keys as $k) { // parse error //eval("\$$k[] = $a[$k];"); // works fine eval("array_push(\$$k, $a[$k]);"); } } I used array_push and it works fine but... 'Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.' It's not a matter of life and death but is there some way to use the [] inside the eval function? thanks in advance for Your help best regards R |
|
|||
|
"R" <ruthless@poczta.onet.pl> wrote in message
news:1153995282.205183.296370@75g2000cwc.googlegro ups.com... > Hi All, > > I'm using eval and arrays in foreach and have trouble with adding > elements > to them - I'm talking about the '[]' operator. > > My code is: > > // creates arrays with the names of columns in keys array > foreach ($keys as $k) { > eval("\$$k = array();"); > } $$k = array(); works fine, no reason to use eval. > // fills dynamically columns' arrays with its values > foreach ($array as $a) { > foreach ($keys as $k) { > // parse error > //eval("\$$k[] = $a[$k];"); Again, just use: $$k[] = $a[$k]; no eval needed here either. > // works fine > eval("array_push(\$$k, $a[$k]);"); And here too: array_push($$k, $a[$k]); > } > } > > I used array_push and it works fine but... Your home assignment is to study a wonderful language feature called "variable variables": http://php.net/manual/en/language.va...s.variable.php -- "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg) |
|
|||
|
R wrote:
> Hi All, > > I'm using eval and arrays in foreach and have trouble with adding > elements > to them - I'm talking about the '[]' operator. > > My code is: > > // creates arrays with the names of columns in keys array > foreach ($keys as $k) { > eval("\$$k = array();"); Why are you using eval here? $$k = array(); is doing what you want. > } > // fills dynamically columns' arrays with its values > foreach ($array as $a) { > foreach ($keys as $k) { > // parse error > //eval("\$$k[] = $a[$k];"); > // works fine > eval("array_push(\$$k, $a[$k]);"); > } > } Stop using eval and you are fine. > > I used array_push and it works fine but... > > 'Note: If you use array_push() to add one element to the array it's > better to use $array[] = because in that way there is no overhead of > calling a function.' > > It's not a matter of life and death but is there some way to use the [] > inside the eval > function? > > thanks in advance for Your help > best regards R Regards, Erwin Moller |
|
|||
|
Kimmo Laine wrote:
> Again, just use: > $$k[] = $a[$k]; PHP5: Fatal error: Cannot use [] for reading but $k is not empty and echo $a[$k] works fine... when I changed above line to: $$k[] = 12345; PHP5: Fatal error: Cannot use [] for reading any idea? best regards R |
|
|||
|
"R" <ruthless@poczta.onet.pl> wrote in message
news:1153998135.175703.276200@m73g2000cwd.googlegr oups.com... > Kimmo Laine wrote: >> Again, just use: >> $$k[] = $a[$k]; > > PHP5: Fatal error: Cannot use [] for reading > > but $k is not empty and > > echo $a[$k] works fine... > > when I changed above line to: > > $$k[] = 12345; > > PHP5: Fatal error: Cannot use [] for reading > > any idea? Damn, I forgot about that. It should be: ${$k}[] = 12345; That should work. -- "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg) |
|
|||
|
Kimmo Laine wrote:
> Your home assignment is to study a wonderful language feature called > "variable variables": > http://php.net/manual/en/language.va...s.variable.php That is so much better and more secure then the \$k construct from another language, the one that would allow you something like: my $class=shift; bless \$k $class; I must say that I really love PHP. If only there was a Comprehensive PHP Archive Network...... Speaking of CPAN, did anybody use this: http://search.cpan.org/~gschloss/PHP...Interpreter.pm -- Mladen Gogala http://www.mgogala.com |