Jump to content

need help with serialize()


anarchoi

Recommended Posts

ok i'm a newbie, and i'm trying to make a small script that allow my members to add Custom Fields to the calendar (i use the calendar to announce gigs/concerts so the custom fields are used to display a list of concert places)

 

The problem is that the custom fields looks like this inside of the database:

a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}

 

Where the numbers are the different custom fields

 

so after searching through vbulletin's calendar.php file and PHP.NET i found out that it looks like this because serialize(); was used

 

is there a quick way to insert a new "custom field" into this serialized thing, while keeping all the other ones intact?? Could anyone please give me an example?

 

thanks for your time :)

Link to comment
https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/
Share on other sites

You would have to unserialize it back to an array, add the fields, and re-serialize it:

<?php
$a = 'a:5:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}';
$ary = unserialize($a);
$ary[] = '999999999999999';
$b = serialize($ary);
echo $b;
?>

Doing the above code would give:

a:6:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";i:6;s:15:"999999999999999";}

 

Ken

your code works, but when i try to apply it to my query instead of $a it just displays a:1:{i:0;s:15:"999999999999999";} :(

 

here is the code i tryed:

$query = "SELECT options from quebec_calendarcustomfield WHERE calendarid='1'";

 

$res = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($res)) {

 

 

$a = $row["options"];

$ary = unserialize($a);

$ary[] = '999999999999999';

$b = serialize($ary);

echo $b;

}

i don't understand why it doesnt work, when i try to echo $a it shows the correct result :

a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}

The string

a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}

is not a valid serialized array. The format is:

a:5  =  5 elements in the array
i:x  = the element x
s:n = length of element x
"xyz" = value of element x

as you can see, the string you show isn't correct, so when you unserialize it you get a null array.

The correct string in your case would be:

a:5:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}

See the difference?

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.