Quick facts for storing arrays into a database.

Whats the harm in storing arrays into a MySql datbase:

  • Its not easy to query
  • Its not straightforward to update the data

Why would I store arrays in MySql ?
In my case I want to save meta data taken from URLs that are passed by end users

  • These values don’t need to be updated.
  • I cant foresee any reason to run complicated queries are this data.
  • It will save me from having to fetch the meta data on every request thus speeding up page load

Different ways to save arrays in MYsql tables:

Using PHP serialize

With the serialize() PHP function you can convert arrays to strings. These strings can easily be stored in MySQL database and converted back to an array with unserialize().


$array = array("value one", "value two", "array(with values)", "another value");
$serialized_array = serialize($array);
// now this data can be stored in the database.
$unserialized_array = unserialize($serialized_array);

Using JSON encode and decode

With the JSON PHP functions you can convert arrays to JSON strings. These strings can easily be stored in MySQL database and converted back to an array with json_decode().


$array = array("value one", "value two", "array(with values)", "another value");
$json_string = json_encode($array);
// now this data can be stored in the database.
$php_array = json_decode($json_string);

Need a website, consultation or customisation? Let’s speak