Posted by Erwin Moller on 07/27/06 10:46
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
Navigation:
[Reply to this message]
|