php code

save arrays into database

i’ve known this for a while but forgot to share it. there is a very easy way you can store array into database as a string. then you can grab the string from the database and convert it back into an array.

code example:

$myarray[] = "meow"; 
$myarray[] = "woof"; 
$myarray[meow] = "cool"; 
$myarray[woof] = "cool"; 
$myarray[thisrules][0] = "Mmmm"; 
$text_myarray = serialize($myarray); 
// store it into my database (depend on the length of your array, declare the column as text or varchar) 
// pull it out of my database, suppose $out_of_my_database stores the text from your database column 
// then $get_myarray = unserialize($out_of_my_database); echo "<pre>"; print_r($get_myarray); 

you should get

    [0] => meow
    [1] => woof
    [meow] => cool
    [woof] => cool
    [thisrules] => Array
            [0] => Mmmm


4 thoughts on “save arrays into database

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s