I have this multidimensional array:
Array
(
[tennis] => Array
(
[3] => Seattle
[4] => WA
[5] => 98109
)
[Middle East] => Array
(
[6] => 2066822513
[7] => 34740 - Georgetown Keg - 1/2 Manny Pale
[0] => Hello World
)
[Florida] => Array
(
[1] => 38380 - Thirteen Coins-Boren
[2] => 125 Boren Ave N
[3] => Seattle
)
)
I am looking for a way to best store it into one single cell in my SQL-DB. The keys of the main array (tennis, middle east, florida) have to be kept. The other keys of the subarrays can be omitted. How can I do that?
I read about the serialize function, and implemented it.
The solution: First I turn the array into a string:
$array = serialize($array);
Then I store the string into the DB cell.
To get back the array, I unserialize the string from the DB cell:
$array = unserialize($array);
And I end up with the same array. Simple
Look at the serialize
function.
I would use JSON since it's more universal.
<?php
$json=addslashes(json_encode($array));
mysql_query("INSERT INTO table (data) VALUES ('$json')");
?>
For really large blobs you could do gzdeflate()
in that case I recommend also saving a checksum.