473,597 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_s tatements_for_r eading_stored_a rray*/

I would think that something like:

eval('$recreate d_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 1774
.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('$recreat ed_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("\$recreat ed_array = $store_array;") ;
p.s. The solution would be be a welcome addition to the PHP
documentatio n 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('$recreat ed_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("\$recreat ed_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
documentatio n 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("\$recreat ed_array = $store_array;") ;
So the only difference was the missing semi-colon?


Not exactly. Compare the two:

eval('$recreate d_array = $store_array');
eval("\$recreat ed_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_arra y = 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()'e d 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
17601
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 array_reverse(). Can anyone tell me why?
1
1650
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 inside loop $q=mysql_query("SHOW FIELDS FROM table" ) or die ("Query failed");
7
1968
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. Unfortunately, most of these problems lie in the ways of architecture. A lot of the tables are designed with no primary keys, and a number of them using the text names in place of the PK-FK relationships. For instances, I have one table called ...
4
5075
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 recreation of the relationships. Code is: Sub Woof() Dim dbs As Database Dim fld As Field, rel1 As Relation, rel2 As Relation Set dbs = CurrentDb
12
1561
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
2204
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 (i.e. index=0) which has the name "title" in place of it ? For example:
0
1318
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 = array('id'=>1, 'username'=>'bob_smith','userpass'=>'psswrd','userlevel'=>'user'); //row
2
1421
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 was easy import array a=array.array("c","mytextgoeshere") my_pointer = a.buffer_info() But then I tried to get the data back... and I just can't find out how.
2
2783
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 snippet below fails to detect arrays :( Thanks!
0
7893
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6698
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.