You are here: Re: using [] inside eval function « PHP Programming Language « IT news, forums, messages
Re: using [] inside eval function

Posted by Kimmo Laine on 07/27/06 10:45

"R" <ruthless@poczta.onet.pl> wrote in message
news:1153995282.205183.296370@75g2000cwc.googlegroups.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.variables.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)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация