How to use php serialize() and unserialize()

后端 未结 10 1715
Happy的楠姐
Happy的楠姐 2020-11-22 05:03

My problem is very basic.

I did not find any example to meet my needs as to what exactly serialize() and unserialize() mean in php? They ju

相关标签:
10条回答
  • 2020-11-22 05:30

    Most storage mediums can store string types. They can not directly store a PHP data structure such as an array or object, and they shouldn't, as that would couple the data storage medium with PHP.

    Instead, serialize() allows you to store one of these structs as a string. It can be de-serialised from its string representation with unserialize().

    If you are familiar with json_encode() and json_decode() (and JSON in general), the concept is similar.

    0 讨论(0)
  • 2020-11-22 05:38

    Yes, I can. Assume we need to track your system means In your system has more than one admin and subadmin, All of these can insert or update or edit any information.Later you need to know who make this change. For solving this problem you need serialize.

      **Explain:**Create a table named history which stores all changes. Each time there is a change insert a new row in this table. It might have this fields:
    
      history(id,target_table(name of the table), target_id (ID of the saved entry),create/edit/change data (serialized data of the saved row),date)
    

    I hope this will help you.

    0 讨论(0)
  • 2020-11-22 05:45

    From http://php.net/manual/en/function.serialize.php :

    Generates a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure.

    Essentially, it takes a php array or object and converts it to a string (which you can then transmit or store as you see fit).

    Unserialize is used to convert the string back to an object.

    0 讨论(0)
  • 2020-11-22 05:46

    Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted.

    it makes really difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized.

    That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare.

    A good example of serialize() and unserialize() could be like this:

    $posts = base64_encode(serialize($_POST));
    header("Location: $_SERVER[REQUEST_URI]?x=$posts");
    

    Unserialize on the page

    if($_GET['x']) {
       // unpack serialize and encoded URL
       $_POST = unserialize(base64_decode($_GET['x']));
    }
    
    0 讨论(0)
  • 2020-11-22 05:47

    A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.


    Take for example this common problem:

    How do I pass a PHP array to Javascript?

    PHP and Javascript can only communicate via strings. You can pass the string "foo" very easily to Javascript. You can pass the number 1 very easily to Javascript. You can pass the boolean values true and false easily to Javascript. But how do you pass this array to Javascript?

    Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 ) 
    

    The answer is serialization. In case of PHP/Javascript, JSON is actually the better serialization format:

    { 1 : 'elem 1', 2 : 'elem 2', 3 : 'elem 3' }
    

    Javascript can easily reverse this into an actual Javascript array.

    This is just as valid a representation of the same data structure though:

    a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}
    

    But pretty much only PHP uses it, there's little support for this format anywhere else.
    This is very common and well supported as well though:

    <array>
        <element key='1'>elem 1</element>
        <element key='2'>elem 2</element>
        <element key='3'>elem 3</element>
    </array>
    

    There are many situations where you need to pass complex data structures around as strings. Serialization, representing arbitrary data structures as strings, solves how to do this.

    0 讨论(0)
  • 2020-11-22 05:47
    preg_match_all('/\".*?\"/i', $string, $matches);
    foreach ($matches[0] as $i => $match) $matches[$i] = trim($match, '"');
    
    
    0 讨论(0)
提交回复
热议问题