Populating dropdown with multilevel json

Asked

Viewed 57 times

1

Gentlemen, I’d like some help. I need to popular a dropdown from a json file

{
  "Categorias":
  [

    {
      "categoria": "Automação",
      "subcategoria": ["supervisório", "programação", "servidor", "hardware"]
    },
    {
      "categoria": "CFTV",
      "subcategoria": ["genetec", "servidor", "cabeamento", "ativo físico"]
    },
    {
      "categoria": "Controle de acesso",
      "subcategoria": ["sistema", "servidor", "controladora", "cabeamento/rede", "equip.físico"]
    }

  ]
}

I managed to make the category key appear on dropdown, but the subcategory occurs an error.

<div class="item form-group">
    <label class='control-label col-md-3 col-sm-3 col-xs-12'>Categoria</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <select class="form-control">
        <?php 
            $arquivo = file_get_contents('teste.json');
            $json = json_decode($arquivo); 
            foreach($json->Categorias as $registro):
                 ?>
                 <option value="1"><?php echo $registro->categoria ?></option>
                 <?php
            endforeach;
        ?>
        </select>
    </div>
</div>          

<div class="item form-group">
    <label class='control-label col-md-3 col-sm-3 col-xs-12'>Sub-categoria</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <select class="form-control">
        <?php 
            $arquivo2 = file_get_contents('teste.json');
            $json2 = json_decode($arquivo); 
           foreach($json2->Categorias as $registro2):

                 ?>
                 <option value="1"><?php echo $registro2->subcategoria ?></option>
                 <?php
            endforeach;
        ?>
        </select>
    </div>
</div>

In the second dropdown, an error message occurs:

(!) Notice: Array to string conversion

How do I create a dropdown only with subcategory options?

2 answers

1

The field subcategoria is a array information, then, need to iterate on it basically, example:

<select class="form-control">
    foreach($data->Categorias as $c): // Categorias
        foreach($c->subcategoria as $d): // subcategoria
           echo "<option value=''>{$d}</option>";
        endforeach;
    endforeach;
</select>

Observing: don’t need to create $arquivo2 = file_get_contents('teste.json'); is redundant if you can use the first normally and the variables can practically be the same.

  • 1

    worked perfectly. Thank you.

  • It is possible for me to display only the values of the first dropdown in the second dropdown?

  • Yes, it does, but that’s another question and another question has to be asked. OK?

0

Bruce, I suggest you make one foreach additional for subcategory within the foreach of the categories, thus:

foreach($json2->Categorias as $registro2):
    foreach($registro2->subcategoria as $registro3):
        echo '<option value="1">'.$registro3.'</option>';
    endforeach;
endforeach;

Browser other questions tagged

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