Create an array from Foreach

I have a Mysql table that contains a JSON data column and a quantity column. The goal is to extract JSON and sum data and build an array in a 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 use to build an 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 return only the first set of records, and the amount is empty, but the JSON data is listed. Appreciate any insight anyone wants to share.

+4
source share
5 answers

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); } 
+22
source

Your $cart variable is overwritten in every loop because you call array() every 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); } 
+7
source
 $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); 
+3
source

Try the following:

 $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 considered the basket as a variable instead of an array

+3
source

a way to approach this is to first create the variable $cart = array(); and then foreach(); I wrote the code below, 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); } 
0
source

Source: https://habr.com/ru/post/1396633/


All Articles