Reputation: 11446
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.
Upvotes: 10
Views: 117056
Reputation: 1430
the way to approach this is first create a variable $cart = array();
and then do foreach();
i have written code below please go through it
foreach($data_main as $transaction_main){
$json_decoded = json_decoded($transaction_main);
$cart[]=array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Upvotes: 0
Reputation: 10634
You need to add []
to $cart
array. WIth each run of foreach you're overwriting the variable $cart.
something like so would work:
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);
}
Or if you wanted the array key to match that of the ID of each row:
Note: You will need to set $id variable somehow above IE: SELECT id, amount
also note that you COULD potentially have issues if integer from id
is non-unique.. eg(1,1,2,3,4,5,6) it will only show the last id of 1
instead of both (since key's are duplicates).
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[$id] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Upvotes: 34
Reputation: 28906
Your variable $cart
is being overwritten in each loop because you are calling array()
each time.
Instead, declare your array outside the loop, and just add values to it as needed:
$cart = 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);
}
Upvotes: 11
Reputation: 9403
Try this :
$cart=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);
}
You were treating the cart as a variable instead of array
Upvotes: 5
Reputation: 17885
$cart = 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);
}
print_r($cart);
Upvotes: 4