tag:blogger.com,1999:blog-31914769265629568762024-02-20T05:12:40.005-08:00The Web NerdPeterhttp://www.blogger.com/profile/12676567305376327317noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-3191476926562956876.post-50390687825826785662009-09-24T05:06:00.000-07:002009-09-24T05:19:59.279-07:00Store array in database - Forget serialize() and unserialize()Many people are struggling with storing their arrays in a MySQL database. There are loads of different blogs and forums posts about this issue, and many of the recommend using the two php functions <a href="http://php.net/manual/en/function.serialize.php">serialize()</a> and <a href="http://www.php.net/manual/en/function.unserialize.php">unserialize()</a>. I have tried several times to use these functions to store my form data arrays in a database, but I simply don’t think they are very useful for this purpose. When you data becomes just a little bit complicated and you have some special characters, the unserialize function fails. I have seen people come up with solutions like base64 encoding the serialized string and so on, but I really don’t find these methods very useful. <br />
<br />
My purpose of this post is to show a much easier way to store arrays in databases. The answer is really simple and I really don’t understand why this isn’t the solution given in many of the online discussions of this particular subject. The answer is the <a href="http://php.net/manual/en/function.implode.php">implode()</a> function pre-build in PHP. The implode() function is a way of transforming multiple strings into one string with some kind of indicator between each of the original strings, so it is possible to bring back the original strings with the <a href="http://www.php.net/manual/en/function.explode.php">explode()</a> function. <br />
<br />
That’s enough talking. Let’s take a look at what the implode() function does:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">$Array = array(‘Peter’, ‘John’, ‘Mark’); // This is our array</span><br />
<span style="font-family: "Courier New", Courier, monospace;"><br />
</span><br />
<span style="font-family: "Courier New", Courier, monospace;">// Different things we could do with the array:</span><br />
<span style="font-family: "Courier New", Courier, monospace;">$ImplodedArray1 = implode(“, ”, $Array);</span><br />
<span style="font-family: "Courier New", Courier, monospace;">$ImplodedArray2 = implode(“ ”, $Array);</span><br />
<span style="font-family: "Courier New", Courier, monospace;">$ImplodedArray3 = implode(“ - ”, $Array);</span><br />
<br />
Example 1, 2 and 3 would return:<br />
1. “Peter, Mark, John”<br />
2. “Peter Mark John”<br />
3. “Peter – Mark - John”<br />
<br />
The other way around you can use the explode() function to make an array out of a text string. Here is how:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">// We wish to separate the text string from example 1, where our separator is a comma and a space.</span><br />
<span style="font-family: "Courier New", Courier, monospace;">$NewArray = explode(“, ”, $ImplodedArray1);</span><br />
<br />
This is the easiest way I know to store an array in a MySQL database. If you disagree or have something else to say, please comment!Peterhttp://www.blogger.com/profile/12676567305376327317noreply@blogger.com0