Charles Smith
Charles Smith

Reputation: 284

Unable to un-serialize an array of arrays with PHP

The array in the database is stored as a serialized string, such as this:

a:1:{i:0;a:4:{s:8:"category";s:26:"Category Name";s:4:"date";s:0:"";s:8:"citation";s:617:"617 Char Length String (shortened on purpose)";s:4:"link";s:0:"";}}

It's structure should resemble the following when unseralized:

array {
    id => array { category => Value, date => Value, citation => Value, link => Value }
}

The php code I'm using is:

$prevPubs = unserialize($result[0]['citations']);

The $result[0]['citations'] is the serialized string. $prevPubs will return false. Which indicates an error if I'm not mistaken.

Any help would be greatly appreciated.

Upvotes: 0

Views: 2569

Answers (3)

Peter
Peter

Reputation: 16923

in order to handle serialized multidmensional arrays and mysql use this:

<?php
//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
?>

i'm pretty sure serialized string in your database is corrupted

Upvotes: 0

Marc B
Marc B

Reputation: 360592

b:0 is boolean:false in serialized format. Unserialize would NOT return that exact string, it'd just return an actual boolean FALSE. This means that whatever you're passing into the unserialize call is not a valid serialized string. Most likely it's been corrupted somehow, causing the unserialize call to fail.

Upvotes: 1

Dmitri Snytkine
Dmitri Snytkine

Reputation: 1126

You have to unserialize the whole string $result = unserialize($serialized);

and then use the $result[0]['citation'] index of the result array.

Upvotes: 0

Related Questions