By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,807 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,807 IT Pros & Developers. It's quick & easy.

Recreating array from SQL field

P: n/a
Hi,

I'm trying to recreate an array from a var_export()'ed array stored in
a database, using eval().

Basically I have something this in my database:

array (
0 => 1,
1 => 2,
2 => array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
If I then read it into a variable:

$stored_array = /*whatever_sql_statements_for_reading_stored_array*/

I would think that something like:

eval('$recreated_array = $store_array');

would do, but it sjust doesn't work, and I'm quite stumped with this
simple task, but I'd appreciate anyone' guidance.

Cheers!

- José

p.s. The solution would be be a welcome addition to the PHP
documentation since there is only a hint of how one could do this but
not a working example.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
.oO(José Luis Ramírez)
I'm trying to recreate an array from a var_export()'ed array stored in
a database, using eval().
eval() is slow. What about serialize()/unserialize()?
I would think that something like:

eval('$recreated_array = $store_array');

would do, but it sjust doesn't work, and I'm quite stumped with this
simple task, but I'd appreciate anyone' guidance.
eval("\$recreated_array = $store_array;");
p.s. The solution would be be a welcome addition to the PHP
documentation since there is only a hint of how one could do this but
not a working example.


There are working examples for eval() in the manual.

Micha
Jul 17 '05 #2

P: n/a
Michael Fesser wrote:
.oO(José Luis Ramírez)
I'm trying to recreate an array from a var_export()'ed array stored ina database, using eval().


eval() is slow. What about serialize()/unserialize()?


Yes. I had already done some tests with serialize()/unserialize(). But
was derailed after finding var_export().

I'm going back to serialize()/unserialize(). Thanks.

I would think that something like:

eval('$recreated_array = $store_array');

would do, but it sjust doesn't work, and I'm quite stumped with this
simple task, but I'd appreciate anyone' guidance.


eval("\$recreated_array = $store_array;");


So the only difference was the missing semi-colon? I'm testing it right
now.

p.s. The solution would be be a welcome addition to the PHP
documentation since there is only a hint of how one could do this butnot a working example.


There are working examples for eval() in the manual.

Micha


I suppose I didn't make myself clear. There ARE a lot of examples on
the eval() page, but not any that tell you explicitly how to recreate a
var_export()'ed array, at least since I checked yesterday, I could be
wrong, maybe I missed the example. Anyway, thanks!

J.L.

Jul 17 '05 #3

P: n/a
.oO(José Luis Ramírez)
Michael Fesser wrote:
eval("\$recreated_array = $store_array;");
So the only difference was the missing semi-colon?


Not exactly. Compare the two:

eval('$recreated_array = $store_array');
eval("\$recreated_array = $store_array;");

Differences in the second:

* double quotes around the string, so embedded variables will be
replaced by their values
* an escaped $ on the first variable, this means it will be passed as-is
to eval(), only $store_array will be replaced by its value
* the missing semicolon

This is the string eval() will finally get and execute:

$recreated_array = array(... stuff from db here ...);
I suppose I didn't make myself clear. There ARE a lot of examples on
the eval() page, but not any that tell you explicitly how to recreate a
var_export()'ed array, at least since I checked yesterday, I could be
wrong, maybe I missed the example. Anyway, thanks!


There is an (bad, but working) example of how to assign a value to a
variable with eval(). It doesn't matter if it's a scalar or an array,
the syntax is the same. You only have to know when it's necessary to
escape a $ and when not.

Micha
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.