SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unserialize function destroys the array

    Hi guys,

    I was hoping someone could help me with this. I have this code:

    PHP Code:
    <?php
    $string 
    = <<<END
     a:29:{i:0;s:1:"2";i:1;s:1:"2";i:2;s:1:"1";i:3;s:1:"1";i:4;s:1:"1";i:5;s:1:"7";i:6;s:1:"2";i:7;s:1:"3";i:8;s:2:"17";i:9;s:1:"1";i:10;s:1:"2";i:11;s:1:"2";i:12;s:1:"1";i:13;s:1:"2";i:14;s:1:"2";i:15;s:2:"31";i:16;s:2:"32";i:17;s:2:"31";i:18;s:1:"2";i:19;s:2
    END;
        echo 
    $string;
        
    print_r(unserialize($string));
    ?>
    It's a pretty simple thing. The $string value is some array that was serialized from another function. When I attempt to unserialize it, it prints nothing! It's like the array just disappeared...is there something wrong with the serialized string or something? I used the serialize function from php...so I can't imagine that causing it?

    This is pretty frustrating

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's not a serialized array. It's missing the } at least. I didn't count if there are the right number of values in it.

  3. #3
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the original array:

    PHP Code:
    $arr = array(
        
    => 2
        
    => 2
        
    => 1
        
    => 1,
        
    => 1
        
    => 7
        
    => 2
        
    => 3
        
    => 17
        
    => 1
        
    10 => 2
        
    11 => 2
        
    12 => 1
        
    13 => 2
        
    14 => 2
        
    15 => 31,
        
    16 => 32
        
    17 => 31
        
    18 => 2
        
    19 => 41
        
    20 => 42
        
    21 => 42
        
    22 => 43
        
    23 => 43
        
    24 => 43
        
    25 => 43
        
    26 => 44
        
    27 => 44
        
    28 => 
        
    );
    $string serialize($arr);
    echo 
    $string;
    print_r(unserialize($string)); 
    The funny thing is it works here...

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not odd behavior. You simply cannot expect unserialize() to work on a string which you chopped the end off of.

    In any case, the array you posted will not produce such a string when you serialize() it.

    Compare them and see.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You're missing a large portion of the serialized array in your original code. It only has 20 values, where your array has 29.

  6. #6
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm. You are right. I just figured out what the problem was. The framework I was using was cutting off the serialized array string.

    Thanks guys.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •