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