Connecting Tech Pros Worldwide Forums | Help | Site Map

using [] inside eval function

R
Guest
 
Posts: n/a
#1: Jul 27 '06
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

Kimmo Laine
Guest
 
Posts: n/a
#2: Jul 27 '06

re: using [] inside eval function


"R" <ruthless@poczta.onet.plwrote in message
news:1153995282.205183.296370@75g2000cwc.googlegro ups.com...
Quote:
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.
Quote:
// 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.
Quote:
// works fine
eval("array_push(\$$k, $a[$k]);");
And here too:
array_push($$k, $a[$k]);
Quote:
}
}
>
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)


Erwin Moller
Guest
 
Posts: n/a
#3: Jul 27 '06

re: using [] inside eval function


R wrote:
Quote:
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.

Quote:
}
// 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.
Quote:
>
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
R
Guest
 
Posts: n/a
#4: Jul 27 '06

re: using [] inside eval function


Kimmo Laine wrote:
Quote:
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

Kimmo Laine
Guest
 
Posts: n/a
#5: Jul 27 '06

re: using [] inside eval function


"R" <ruthless@poczta.onet.plwrote in message
news:1153998135.175703.276200@m73g2000cwd.googlegr oups.com...
Quote:
Kimmo Laine wrote:
Quote:
>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)


Mladen Gogala
Guest
 
Posts: n/a
#6: Jul 27 '06

re: using [] inside eval function


Kimmo Laine wrote:
Quote:
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
Closed Thread