1

I have a string that I get from the database, it's saved as a value in a column, is something like this:

a:6:{s:4:"host";s:8:"somehost";s:4:"user";s:8:"someuser";s:4:"pass";s:8:"somepass";s:3:"enc";b:0;s:4:"port";s:3:"xxx";s:5:"buggy";b:0;}

That's what I get but I need it in a PHP array formated like this:

array(6) { ["host"]=> string(8) "somehost" ["user"]=> string(8) "someuser" ["pass"]=> string(8) "somepass" ["enc"]=> bool(false) ["port"]=> string(3) "xxx" ["buggy"]=> bool(false) } 

It's an old system and they save that data as above, but now I need to read it and I don't know how to handle it. It's curious that it also saves the length of every field (like if they save the array). Is there a way in PHP to convert it? I checked the database and the type of the field is varchar. Thank you so much

Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115

1 Answers1

1

This is bound to be a duplicate but they're hard to find without unserialize() or serialize() in the title, but that's an array from serialize():

print_r(
unserialize('a:6:{s:4:"host";s:8:"somehost";s:4:"user";s:8:"someuser";s:4:"pass";s:8:"somepass";s:3:"enc";b:0;s:4:"port";s:3:"xxx";s:5:"buggy";b:0;}')
);

Yields:

Array
(
    [host] => somehost
    [user] => someuser
    [pass] => somepass
    [enc] =>
    [port] => xxx
    [buggy] =>
)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87