473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using [] inside eval function

R
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

Jul 27 '06 #1
5 2070
"R" <ru******@poczta.onet.plwrote in message
news:11**********************@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
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Jul 27 '06 #2
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
Jul 27 '06 #3
R
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

Jul 27 '06 #4
"R" <ru******@poczta.onet.plwrote in message
news:11**********************@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
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Jul 27 '06 #5
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
Jul 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Julian Smith | last post by:
I've been playing with a function that creates an anonymous function by compiling a string parameter, and it seems to work pretty well: def fn( text): exec 'def foo' + text.strip() return foo ...
2
by: Billy | last post by:
I want to make a calendar and add a hyperlink to the days inside the calendar. The source code is below: <body> <SCRIPT LANGUAGE="JavaScript"> var now = new Date();
12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
10
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
1
by: Bob Tinsman | last post by:
I've been interested in E4X because my company has an XML schema that we usually manipulate through a Java mapping generated by Castor, which I think is fairly tedious, and which means you have to...
1
by: Joel Byrd | last post by:
I've been using an eval() statement, which has been working fine until I put it inside of a function. Is this a known problem, and is there a known solution/work-around to this?
1
by: sck10 | last post by:
Hello, I am calling a function using vb and want to convert it to c#. When moving from vb 1.1 to vb 2.0, I can use the following: text='<%# AddBlankRowMenu(Eval("strMenuType")) %>' to call the...
2
by: Daz | last post by:
Hi everyone. Sorry for the confusing subject, I couldn't think how best to word it. What I would like to know, is if there is an equivilant to this code, in using JSON. <script...
1
by: semomaniz | last post by:
I have a button inside a repeater which is supposed to open a popup when clicked. But when i click on the button my modalpopup does not open. The strange thing is on the code provided below if i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.