1

I want to save a long array in mysql database and when i read that array back from mysql database, i want that array back in proper array format. Is this possible?

Other threads are suggesting to convert array into string using serialise and explode. But will they help me to get proper array back? Thanks.

Mercurial
  • 3,615
  • 5
  • 27
  • 52

2 Answers2

3

You can try it yourself.

As an alternative to serailize(), you can use json_encode().

sidenote: reverse of serialize() is unserialize(), not explode().

sidenote: reverse of json_encode() is json_decode().

sidenote: Very worthwhile to read: Discussion of json_encode() vs serialize()

Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Usually, serialize and unserialize functions are used for such purposes.

With serialize you can convert your array to a string and than get an array by applying unserialize on string you get from serialize.

And about explode: you may use it too, but than you will need to use implode function to serialize array. But it will work with simplest one dimensional arrays only:

implode(",", array("val1", "val2", "val3")) = "val1,val2,val3"
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • 1
    agree that `implode()` can be helpful for very simple array, but it does not handle array elements that contents `,`, which will produce unwanted results. Still, `serialize()` & `json_encode()` are preferred. – Raptor May 09 '13 at 08:49