简体   繁体   中英

PHP Unserialize Data From Mysql and Create Array

I have the two columns in my table within Mysql. The Columns are Amount and NewObject which contains a serialized array structure like this:

{"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"}}

The other column is amount. I am running this query to pull out all the rows that match the ID:

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

I then want to extract the 'Name' field from the serialized array NewObject (example: "Name":"newnamehereagain") and once I have the name, create an array with this name, and then add the 'Amount' from the database column to this name. Can this be accomplished?

Here is what I have attempted (no joy):

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

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

Your "NewObject" column appears to contain a nested hash which has been JSON-encoded. You should use json_decode to turn it back into a nested array structure, modify the value, and then reencode it via json_encode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM