Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing associative array in MySQL DB

I'd like to store associative array containg mostly strings and integers as values in db.

I was thinking:

  • implode/explode - need to find delimiter, which won't be in values - since it's almost user generated, not safe
  • XML - feels to heavy for the job (creating/reading values)
  • json - if i need only to work with json_decode/json_encode, seems perfect

What do you think?

Please, do not forward me to other questions like this on SO, I've read most of them and I'm still not sure :)

like image 237
Adam Kiss Avatar asked Aug 30 '25 18:08

Adam Kiss


2 Answers

I think serialize: http://php.net/manual/en/function.serialize.php

like image 187
Mchl Avatar answered Sep 02 '25 07:09

Mchl


You could either serialize() the array, or json_encode() it when writing to the database, and json_decode() when fetching from the database.

like image 45
Martin Bean Avatar answered Sep 02 '25 06:09

Martin Bean