Store array in db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Store array in db

Post by anjanesh »

Is there anyway to store this in a mysql table such that when retrieving it in mysql_fetch_assoc() I should be able to use the result as an array

Code: Select all

$array=array(
array("Key1"=>"Value1","Key2"=>"Value06","Key3"=>"Value11"),
array("Key1"=>"Value2","Key2"=>"Value07","Key3"=>"Value12"),
array("Key1"=>"Value3","Key2"=>"Value08","Key3"=>"Value13"),
array("Key1"=>"Value4","Key2"=>"Value09","Key3"=>"Value14"),
array("Key1"=>"Value5","Key2"=>"Value10","Key3"=>"Value15")
);

Code: Select all

$row=mysql_fetch_assoc($res);
$arr=$row['array']
echo $arr[2]['Key2'];
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you can use [php_man]serialize[/php_man] to store any complex structure in a string. Just make sure you have that column in db large enough.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if the structure of it is pretty much the same, I wouldn't store such a large amount of data in one single field. Instead, I'd break the structure apart into it's own columns or table, whichever is better for the situation.
Post Reply