How can I pull a specific part out of a serialized data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tasha whitman
    New Member
    • May 2011
    • 3

    How can I pull a specific part out of a serialized data?

    I'm new to PHP and I've been trying to wrap my brain around this to no avail. Basically in this program I have groups and the group id is apparently stored as serialized data like this:

    Code:
    a:1:{i:0;s:2:"4";}
    The only number i need is "4".

    But I have no idea how to pull that out. IS this easier than it feels?

    Thank you!
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    If always wrapped in double quotes and they are the only ones
    Code:
    $str = 'a:1:{i:0;s:2:"4";}'
    $id = substr($str,strpos($str,'"')+1,1);
    Not tested this, but the idea is strpos gives position of first double quote.
    Add 1 to this and substr returns 1 character of string from here.

    Comment

    • tasha whitman
      New Member
      • May 2011
      • 3

      #3
      Thank you. That doesn't seem to work for me though. I should probably give more info. The SQL database I am pulling out of is labeled

      _group
      with
      Code:
      a:1:{i:0;s:1:"4";}
      in it

      On the php end where i am trying to get that number into looks like this
      Code:
      $userPerms = array(
           "role" => "", //role id
      When I stick the number "4" in there it works! but for only one group. and that number can change with what group that person wants. To pull out the data for the table, the code already does this:

      Code:
      "website" => $this->userInfo['website']
      Which spits out exactly what's in there, so i've tried different variations of it using what you suggested. but to no avail.

      I've tried this

      Code:
      $roleout = $this->userInfo['_group']
      			{
      			$role = unserialize($roleout);
      			$role2 = substr($role,strpos($role,'"')+1,1);
      			}
      			;
      			$userIsAdmin = false;
      			$userPerms = array(
      				"role" => $role2, //role id
      but I think i'm just shooting blanks and hoping one is real. lol

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Try Using explode function.

        follow the link to understand detail

        Comment

        • tasha whitman
          New Member
          • May 2011
          • 3

          #5
          Okay I figured it out. It was a mixture of discovery and newbie error.

          Code:
          $roleout = $this->userInfo['_group'];
          			$role2 = unserialize($roleout);
          			
          			
          			$userIsAdmin = false;
          			$userPerms = array(
          				"role" => $role3 = ($role2[0]), //role id
          and that was it. I was putting the second part in {} which was messing it all up. And then I needed to learn more about array's and how to pull out the correct position. Newbie right! Went through 2 php books, lots of research, lynda.com and unserialize.net to figure it all out.

          thank you for all the help!

          Comment

          Working...