I have a Mysql table which contains a column of JSON data and a column with an amount. The goal is to extract the JSON data and the amount and build an array within the foreach loop. Here is my code:
$sql = "SELECT `Amount`, `NewObject` FROM `mb_cart` WHERE `MyID` = '$id'";
$data_main = $db->query($sql);
Here is my statement that I am using to build the array:
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
However when I run this, I am only returning only the first record set and the amount is blank, but the JSON data is listed. Appreciate any insight anyone cares to share.
$db->query($sql);return? An array or an iterator? See alsoarray_pushand the note there,foreachwith a reference anditerator_to_array.