Foreach in JSON file

1

I have a JSON return, but I am not able to make a foreach (PHP) on it, it gives this error:

  

Invalid argument supplied for foreach ()

Below is JSON :

{
  "categorias": {
    "categoria": [
      {
        "id": "1",
        "nome": "Chás e Ervas",
        "subcategorias": [
          {
            "id": "1",
            "nome": "Chás",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "2",
            "nome": "Chimarrão",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      },
      {
        "id": "2",
        "nome": "Condimentos e Especiarias",
        "subcategorias": [
          {
            "id": "3",
            "nome": "Azeites",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "4",
            "nome": "Caldos",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "5",
            "nome": "Conservas",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      },
      {
        "id": "3",
        "nome": "Diet e Light",
        "subcategorias": [
          {
            "id": "6",
            "nome": "Açaí e Guaraná",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "7",
            "nome": "Adoçantes",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      }
    ]
  }
}
    
asked by anonymous 21.05.2017 / 22:48

1 answer

1

To read this would look like this:

Example minimum :

$dados = json_decode($json);    

foreach ($dados->categorias->categoria as $c) 
{
    echo $c->id;
    echo ' - ';
    echo $c->nome;
    echo '<br>';
    foreach ($c->subcategorias as $s) 
    {
        echo ' - ' . $s->id;
        echo ' - ';
        echo $s->nome;
        echo ' - ';
        echo $s->descricao;
        echo '<br>';
    }
    echo '<br>';
    echo '<br>';

}

$dados = json_decode($json);


foreach ($dados->categorias->categoria as $c) 
{
    echo $c->id;
    echo ' - ';
    echo $c->nome;
    echo '<br>';
    foreach ($c->subcategorias as $s) 
    {
        echo ' - ' . $s->id;
        echo ' - ';
        echo $s->nome;
        echo ' - ';
        echo $s->descricao;
        echo '<br>';
    }
    echo '<br>';
    echo '<br>';

}

Full tag :

<?php 

$json = '{
  "categorias": {
    "categoria": [
      {
        "id": "1",
        "nome": "Chás e Ervas",
        "subcategorias": [
          {
            "id": "1",
            "nome": "Chás",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "2",
            "nome": "Chimarrão",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      },
      {
        "id": "2",
        "nome": "Condimentos e Especiarias",
        "subcategorias": [
          {
            "id": "3",
            "nome": "Azeites",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "4",
            "nome": "Caldos",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "5",
            "nome": "Conservas",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      },
      {
        "id": "3",
        "nome": "Diet e Light",
        "subcategorias": [
          {
            "id": "6",
            "nome": "Açaí e Guaraná",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          },
          {
            "id": "7",
            "nome": "Adoçantes",
            "descricao": "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          }
        ]
      }
    ]
  }
}';


$dados = json_decode($json);


foreach ($dados->categorias->categoria as $c) 
{
    echo $c->id;
    echo ' - ';
    echo $c->nome;
    echo '<br>';
    foreach ($c->subcategorias as $s) 
    {
        echo ' - ' . $s->id;
        echo ' - ';
        echo $s->nome;
        echo ' - ';
        echo $s->descricao;
        echo '<br>';
    }
    echo '<br>';
    echo '<br>';

}
    
21.05.2017 / 23:41