开发者

Storing associative array in MySQL DB

开发者 https://www.devze.com 2023-01-08 19:47 出处:网络
I\'d like to store associative array containg mostly strings and intege开发者_如何学Crs as values in db.

I'd like to store associative array containg mostly strings and intege开发者_如何学Crs 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 :)


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


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


It really all depends on the way in which you want to retrieve this data once you DO get it in the database. Sure you could serialize an array and put it in your field, but what if you want to perform queries with the data in that array; you'd have to pull the serialized array out, perform PHP functions, and then do your query.

You need to explain what it is you want to achieve with this associative array data.


There's also the option of creating a table with key and value columns, and storing each array element individually in that table. It might seem excessive, but could prove useful depending on what you want to do with the data

0

精彩评论

暂无评论...
验证码 换一张
取 消