help like How to traverse this JSON array in PHP [duplicated]

-2

Hello how can I do to get the name of each of the ones that appear in this json array with php. and thanks for the help

"genres": [
    {
        "id": 12,
        "name": "Aventura"
    },
    {
        "id": 878,
        "name": "Ciencia ficción"
    },
    {
        "id": 28,
        "name": "Acción"
    }
],
    
asked by jhon sanchez 17.09.2018 в 22:25
source

2 answers

0

Try this:

foreach($genres->data as $genre) {

  echo $genres->id. "\n";
  echo $genres->name;

}   

The variable $ genres is the one that contains your json

    
answered by 17.09.2018 / 22:43
source
0

If you want to travel only with php, you could go through it as an arrangement, and work it in the following way:

<?php
  $genres = [
              [
                "id" => 12,
                "name" => "Aventura"
              ],
              [
                "id" => 878,
                "name" => "Ciencia ficción"
              ],
              [
                "id" => 28,
                "name" => "Acción"
              ]
            ];

  foreach ($genres as $g) {
    echo $g['name'];
    echo '</br>';
  }

?>
    
answered by 17.09.2018 в 22:46