0

I have to populate a json with PHP. I have this structure:

$request = array(
      "api_uid" => "000000", 
      "api_key" => "xxxxxx", 

      "lista_articoli" => array(

      //loop 


      array(
"nome" => "Acconto",
      "descrizione" => "Acconto per la festa del " .$datafesta,
      "prezzo_lordo" => $importo,
      "cod_iva" => 0 
        )

   // end loop

I try to use while inside array, but it's an error:

while($row = $tipologia->fetch(PDO::FETCH_ASSOC)) {

      array("nome" => $row['nome'],
      "descrizione" => $row['desc'],
      "prezzo_lordo" => $row['prezzo_lordo'],
      "cod_iva" => 0 
        ),
                     }

How can i loop my data in correct way inside array?

1
  • you can not loop in inside array Commented Jun 24, 2017 at 14:01

1 Answer 1

2

You can't loop inside array.You need to create array and assign value to them by keys in loop.Try like below :

while ($row = $tipologia->fetch(PDO::FETCH_ASSOC)) {
    $request['lista_articoli'][] = [
        'nome' => $row['nome'],
        'descrizione' => $row['desc'],
        'prezzo_lordo'] => $row['prezzo_lordo'],
        'cod_iva' => 0,
    ];
}
Sign up to request clarification or add additional context in comments.

7 Comments

This will just keep overriding the values, you probably want to add an array on each iteration.
no that is not what he asked...He just showed one array and assigning inside array which is wrong..
Thank you, but I can not understand where to put your code into my structure
you can put anywhere where is appropriate..Please accept the answer by click the tick mark @frwebdev
i try to put your code inside my loop area, but i still have the same error, because i have array right after ...$request = array(.... Sorry for my low level in php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.