473,396 Members | 2,018 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,396 software developers and data experts.

Recreating array from SQL field

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
3 1763
.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
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
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
1
by: pauld | last post by:
from a MySQL DB i want to get a multidimensional array that i can loop through either key =field name value = array of ENUM options or array =field name, array= ENUM options and increment x...
7
by: Andante.in.Blue | last post by:
Hello everyone! I've been working with a problematic legacy database for a while. While I am still fairly new to Access, the more I work with the database, the more problems I've uncovered. ...
4
by: Jenni | last post by:
Hopefully someone out there can help. I am currently trying to write some code to allow me to delete a table, then recreate it and re-establish the relationships. I seem to have hit a snag in the...
12
by: Alexander Stippler | last post by:
Hello, I've got a quite simple question. I want to have something like int main() { double array = {{1,2,3}, {4,5,6}, {7,8,9}}; double **field; field = array;
4
by: bienwell | last post by:
Hi all, Data displayed on the datalist control is bound by the column name of the dataset like this : <%# DataBinder.Eval(Container.DataItem, "title")%> Could I use an element of the array...
0
by: josh.23.french | last post by:
Here's the code i have: $db = array(); //main array $db = array(); //table `main` $db = array('id'=>0, 'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin'); //row $db =...
2
by: buffinator | last post by:
I have an application that has to send a string as a pointer to memory, and then another one that has to retriece it and fetch the string. Converting the string to an array and sending the pointer...
2
by: Mikhail Teterin | last post by:
Hello! I'm going through the fields of a class one at a time and need to handle differently depending on whether they are arrays or scalars. What's the right way to make the distinction? The...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.