1

I have this complicated JSON which I want to parse it in PHP so that I can construct dropdown.

I want to construct dropdown as

<select name="select-box">
  <option value="apple">apple</option>
    <option value="banana">banana</option>
    <option value="kiwi">kiwi</option>
</select>

Right now I'm doing something like this which is not working

$json = '[
   {
      "class":"autocomplete",
      "name":"autocomplete-1481957691348-preview",
      "value":"there is nothing like me",
      "id":"autocomplete-1481957691348-preview",
      "type":"autocomplete",
      "tag":"INPUT"
   },
   {
      "class":"form-control",
      "name":"file-1481957721195-preview",
      "value":"",
      "id":"file-1481957721195-preview",
      "type":"file",
      "tag":"INPUT"
   },
   {
      "select-box":[
         {
            "selectName":"apple",
            "optionValue":"apple"
         },
         {
            "selectName":"banana",
            "optionValue":"banana"
         },
         {
            "selectName":"kiwi",
            "optionValue":"kiwi"
         }
      ]
   },
   {
      "class":"form-control",
      "name":"select-1481957826542-preview",
      "value":"jadu",
      "id":"select-1481957826542-preview",
      "type":"select",
      "tag":"SELECT"
   }
]';

My interest is this object from above JSON

{
          "select-box":[
             {
                "selectName":"apple",
                "optionValue":"apple"
             },
             {
                "selectName":"banana",
                "optionValue":"banana"
             },
             {
                "selectName":"kiwi",
                "optionValue":"kiwi"
             }
          ]
       }

But I don't want to parse this separately. Instead I want to parse the whole JSON

$arrData    =  json_decode($json);
  echo '<select name=''>';
    foreach($arrData as $key=>$objData){
      echo '<option value='".$objData->selectName."'>'.$objData->selectName.'</option>';
    }
echo '</select>';
Ali
  • 2,993
  • 3
  • 19
  • 42
Dilip G
  • 489
  • 2
  • 7
  • 15

4 Answers4

1

You could pass true as a second parameter to json_decode to return the data as arrays rather than objects. Then you could do the following:

// Decode all of the JSON string and store into variable
$decodedJson = json_decode($json, true);

// Retrieve only the select box related data from all of the decoded JSON data
$selectOptions = $decodedJson[2]['select-box'];

echo '<select name="">';

foreach($selectOptions as $selectOption) {
    echo '<option  value="'.$selectOption['selectName'].'">'.$selectOption['selectName'].'</option>';
}

echo '</select>';

Also I've copied what you did where you have used the 'selectName' value on each option as the select box option value as well as name. Not sure if you want to change it to use the 'optionValue' value instead but I thought I'd raise it just in case.

Hope this helps!

Ben Plummer
  • 449
  • 3
  • 9
1

You had some mistakes in your code like:

echo '<select name=''>';

should be:

echo '<select name="">';

You can convert the object into an array and parse it like this:

<?php 
 $json = '[
   {
      "class":"autocomplete",
      "name":"autocomplete-1481957691348-preview",
      "value":"there is nothing like me",
      "id":"autocomplete-1481957691348-preview",
      "type":"autocomplete",
      "tag":"INPUT"
   },
   {
      "class":"form-control",
      "name":"file-1481957721195-preview",
      "value":"",
      "id":"file-1481957721195-preview",
      "type":"file",
      "tag":"INPUT"
   },
   {
      "select-box":[
         {
            "selectName":"apple",
            "optionValue":"apple"
         },
         {
            "selectName":"banana",
            "optionValue":"banana"
         },
         {
            "selectName":"kiwi",
            "optionValue":"kiwi"
         }
      ]
   },
   {
      "class":"form-control",
      "name":"select-1481957826542-preview",
      "value":"jadu",
      "id":"select-1481957826542-preview",
      "type":"select",
      "tag":"SELECT"
   }
]';

$arrData    =  json_decode($json, true);
  echo '<select name="">';
    foreach($arrData as $objData){
        $for_select = $objData['select-box'];
        if(is_array($for_select)){
            foreach($for_select as $sel){
                echo '<option value="'.$sel['selectName'].'">'.$sel['selectName'].'</option>';
            }
        }
    }
  echo '</select>';
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
1

You have some errors with quotes and also you need to iterate in your nested json if it'not empty as follow

$arrData    =  json_decode($json);
echo '<select name="Select-box">';
foreach($arrData as $key=>$objData){
    if(!empty($objData->{'select-box'})) {
          foreach($objData->{'select-box'} as $select){

              echo '<option value="'.$select->optionValue.'">'.$select->selectName.'</option>';
          }
    }
}
echo '</select>';

//print
//<select name="select-box"><option value="apple">apple</option><option value="banana">banana</option><option value="kiwi">kiwi</option></select>

live sample

If you don't know property name because it changes all the time you can do as follow

$arrData    =  json_decode($json);
echo '<select name="select-box">';
foreach($arrData as $key=>$objData){
    foreach($objData as $data) {
        if(is_array($data)) {
             foreach($data as $select){

                  echo '<option value="'.$select->optionValue.'">'.$select->selectName.'</option>';
             }
        }
    }
}

live sample

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • if there is `dynamic` name for `select-box` in that scenario how can i do this. for example instead of `select-box` it is `veg` then how can i do it dynamically – Dilip G Dec 17 '16 at 09:51
  • @DilipG i've updated my answer as your request – Fabio Dec 17 '16 at 10:13
0
$arrData    =  json_decode($json, TRUE);
echo '<select name="">';
foreach($arrData as $key=>$objData){
  foreach($objData['select-box'] as $key2=>$objData2){
                 echo '<option value='.$objData2['optionValue'].'>'.$objData2['selectName'].'</option>';
  }
}
echo '</select>';
Amruth
  • 5,792
  • 2
  • 28
  • 41