You need to add the [] array to $cart . Each time you run foreach, you overwrite the $ cart variable.
something like this will 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 want the array key to match the identifier identifier of each row:
Note. . You will need to set the $ id variable somehow above IE: SELECT id, amount also note that you may have problems if the integer from id not unique .. for example (1, 1,2,3,4, 5,6), it will show only the last id 1 instead of both (since the key is a duplicate).
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); }
source share