Foreach in JSON file

Asked

Viewed 570 times

1

I have a comeback JSON, but I’m not getting a foreach (PHP) in it, keeps giving this error:

Invalid argument supplied for foreach()

Follows below the 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."
          }
        ]
      }
    ]
  }
}
  • This is JSON, right? And your PHP code, you can post it?

1 answer

1

To read this would that way:

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>';

}

Code complete:

<?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>';

}

  • 1

    Thanks Virgilio, I’ll test it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.