PHP从Mysql和创建数组反序列化数据

时间:2012-02-14 19:39:40

标签: php arrays serialization

我在Mysql中的表中有两列。列是Amount和NewObject,它包含一个这样的序列化数组结构:

{"type":"Sanction","data":{"MyID":-1,"Type":{"type":"Translation","data":{"code":"STATE","codeType":"Purchase"}},"Name":"newnamehereagain","Org":"60066","StartDate":"2012-02-15","EndDate":"2012-02-16","CloseDate":null,"EnteredDate":"2012-02-14 15:33:58","PacketSentDate":"2012-02-14 15:33:58","CancelledDate":null,"CancellationReason":null,"MyBigID":"004061","Countries":null,"SiteName":"nert","AddressLine1":"98 Patterson Road","AddressLine2":"","City":"Lawrenceville","State":"GA","Zip":"30044","Participants":null,"AdditionalInsured":"","Comments":null,"Program":"TT","Levels_SanctionLevel_array":"IT03,TLEVEL10"}}

另一栏是金额。我正在运行此查询以提取与ID匹配的所有行:

$sql2 = $db->query("SELECT `Amount`,`NewObject` FROM `mb_cart` WHERE `PersonID` = '$id'");

然后我想从序列化数组NewObject中提取'Name'字段(例如:“Name”:“newnamehereagain”),一旦我有名字,创建一个具有此名称的数组,然后添加'Amount'从数据库列到此名称。这可以实现吗?

这是我的尝试(没有快乐):

foreach ($sql2 as $values)
{
    $value = explode(",", $values);

foreach ($value as $v)
   {
   array($amount, $v);
   }
}

1 个答案:

答案 0 :(得分:1)

您的“NewObject”列似乎包含已经过JSON编码的嵌套哈希。您应该使用json_decode将其重新转换为嵌套数组结构,修改该值,然后通过json_encode重新编码。