10

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.

1
  • What does $db->query($sql); return? An array or an iterator? See also array_push and the note there, foreach with a reference and iterator_to_array. Commented Feb 15, 2012 at 14:03

5 Answers 5

34

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I only get $names[$row["nombre"]] = array('id' => $row["id"]); -------Result---------> key = Selección perfil , value = Array , why in value get array ?
You need to add [] to $cart array <= this helps me a lot. Thanks bro :)
11

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);
}

Comments

5

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

Comments

4
$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);

Comments

0

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.