what's the point of serializing arrays to store them in the db?

前端 未结 5 706
感情败类
感情败类 2021-01-16 07:21

I see that people store arrays as like:

a:6:{i:0;s:5:\"11148\";i:1;s:5:\"11149\";i:2;s:5:\"11150\";i:3;s:5:\"11153\";i:4;s:5:\"11152\";i:5;s:5:\"11160\";}

5条回答
  •  天命终不由人
    2021-01-16 07:45

    1. Not all DB servers have an array type (mysql being one)
    2. if you have a csv list, you have to explode it, or loop over it to recreate the array
    3. multi dimensions aren't possible

    With serialize and unserialize you are using native php functions that run faster than php based loop constructs etc. serialize and unserialize are the way to go if you absolutely positively have to store an array in the db (like for sessions). Otherwise you may want to reconsider your application design to not store php array in the database. It may cause problems down the road if you try to access the data with a language other than php.

提交回复
热议问题