What is data serialization ?

后端 未结 5 1637
后悔当初
后悔当初 2021-01-31 02:33

First of all, I could not able to get clear definition of it from WikiPedia or even from serialize function in the PHP manual. I need to know some cases we need the term seriali

5条回答
  •  失恋的感觉
    2021-01-31 03:16

    Serialization is the process of turning data (e.g. variables) into a representation such as a string, that can easily be written and read back from for example a file or the database.

    Use cases? There are many, but generally it revolves around the idea of taking a complex, nested array or object and turning it into a simple string that can be saved and read later to retrieve the same structure. For example, provided you have in php:

    $blub = array();
    $blub['a'] = 1;
    $blub['a']['b'] = 4;
    $blub['b'] = 27;
    $blub['b']['b'] = 46;
    

    Instead of going through every array member individually and writing it one could just:

    $dataString = serialize($blub);
    

    And the serialized array is ready to be written anywhere as a simple string, in such a way that retrieving this string again and doing unserialize() over it gets you the exact same array structure you had before. Yes, it's really that simple.

提交回复
热议问题