I think you can just serialize() the array. See the notes on the man page here:
http://us2.php.net/seriali
Be sure to escape the serialized data before inserting it - use mysql_real_escape_string()
Best, ~Ray
Main Topics
Browse All TopicsHello experts,
This is my first foray into multidimensional arrays. Can someone tell me what is the best way to store values in a multidimensional an array in a database. For single arrays I use 'implode' and save as a string and if I need to access the values I use 'explode'. Can this be done for multidimensional arrays and is this the best way of doing it or do I need to use 'serialize'? I've enclosed a snippet of the code I've started. Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I think you can just serialize() the array. See the notes on the man page here:
http://us2.php.net/seriali
Be sure to escape the serialized data before inserting it - use mysql_real_escape_string()
Best, ~Ray
>> what is the best way to store values in a multidimensional an array in a database
The best way is to store it in columns in a table:
create table Messages (userid int,note text);
You should probably have some additional columns, maybe a timestamp? If each user can have only one note, you should have an index on the userid column.
thanks for the help everyone.
I've set it up so the user can have multiple notes, say maximum of 10 that they can add/remove from. The table has two columns - user_id, note. 'user_id' is unique int, while 'note' is text and contains all values of the array.
I will do some reading on serialize and give it a try and get back with results
@allanch08: You may be asking a "loaded" question here. If you have only a small amount of data and you do not need to search it, almost any factually correct answer about how to store it will be acceptable. By "small amount" I am thinking a few thousand records a day at most. However if you need the data base capabilities, then you want to think about the structure of the data and in that case, serialize() is maybe not the best approach, since serialize() removes the structure and converts it into a string (albeit a reversable process). If the underlying question is deeper than what you've posted here, consider "normalization."
http://lmgtfy.com?q=why+sh
Best, ~Ray
thanks ray. It's just a small amount of data I'm working with, I thought about using tables in a db but that would be too much for it. This is the code I've got so far and it prints out:
0 = 00001Hello1 = 00002Goodbye2 = 00005Good morning3 = 00007Good evening
I understand that there are three numeric keys [1][2][3] with corresponding values and this is a big string but how can I format it so I can change it back into an array?
thanks ray that code is really helpful. I'm going to study it a bit more. one thing I stumbled upon when learning more about serialize was in this blog:
http://davidwalsh.name/php
any views on this? thanks
There is no known bug in unserialize(), the problem described at the blog mentioned above was probably because magic_quotes was enabled, so that addslashes() (or mysql_real_escape_string()
thanks, I did notice that using base64encode did generate a lot of characters. I also been testing serialize/unserialize and all works. another question is have is how can i refer to associative keys. to explain better in the code below the varialbe '$note' has two values for each array. so how can i just print the 'user_id' in each case? thanks
Business Accounts
Answer for Membership
by: 930913Posted on 2009-10-05 at 15:40:45ID: 25500520
You can nest two for loops each one counting a dimension serialising it as you said.
As long as it is read back in the same way, it doesn't matter how you save it.