using [] inside eval function

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-27-2006
R
 
Posts: n/a
Default using [] inside eval function

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

Reply With Quote
  #2 (permalink)  
Old 07-27-2006
Kimmo Laine
 
Posts: n/a
Default Re: using [] inside eval function

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


Reply With Quote
  #3 (permalink)  
Old 07-27-2006
Erwin Moller
 
Posts: n/a
Default Re: using [] inside eval function

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
Reply With Quote
  #4 (permalink)  
Old 07-27-2006
R
 
Posts: n/a
Default Re: using [] inside eval function

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

Reply With Quote
  #5 (permalink)  
Old 07-27-2006
Kimmo Laine
 
Posts: n/a
Default Re: using [] inside eval function

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


Reply With Quote
  #6 (permalink)  
Old 07-27-2006
Mladen Gogala
 
Posts: n/a
Default Re: using [] inside eval function

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:29 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0