user3417863
user3417863

Reputation: 3

PHP serialize array

I'm developing a wordpress theme which uses GBS. In the database field values are stored like this:

a:1:{i:0;s:7:"Virtual";}

Which clearly uses serialize, However when I store a meta value and use php to serialize I get this:

s:24:"a:1:{i:0;s:7:"Virtual";}";

Is there any way to not get the string count of the entire array? Here's the code I used:

update_post_meta( $post_id, 'location', serialize(array('Virtual')) );

Upvotes: 0

Views: 588

Answers (3)

Victory
Victory

Reputation: 5890

You can explode by : slice off the first two bits of the array and then implode back on ":" like so

$str = 's:24:"a:1:{i:0;s:7:"Virtual";}";';
$expected = '"a:1:{i:0;s:7:"Virtual";}";';
$bitsOfSerial = explode(":", $str);
$noStringLengthBits = array_slice($bitsOfSerial, 2);
$actual = implode(":", $noStringLengthBits);
assert($expected === $actual);
echo $actual;

Upvotes: 0

Olvathar
Olvathar

Reputation: 551

update_post_meta seems to serialize whatever you send (a string of a serialized array in this time). Did you tried to change the thirs argument to just array('Virtual');??

Upvotes: 0

John V.
John V.

Reputation: 4670

It looks like it's being double serialized, meaning update_post_meta serializes it already. Try it without your explicit call to serialize:

update_post_meta( $post_id, 'location', array('Virtual'));

Upvotes: 1

Related Questions