我有这个复杂的.pdf
,我想在JSON
解析它,以便我可以构建PHP
。
我想构建下拉列表
dropdown
现在我正在做这样无法正常工作的事情
<select name="select-box">
<option value="apple">apple</option>
<option value="banana">banana</option>
<option value="kiwi">kiwi</option>
</select>
我感兴趣的是$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"
}
]';
以上的object
JSON
但我不想单独{
"select-box":[
{
"selectName":"apple",
"optionValue":"apple"
},
{
"selectName":"banana",
"optionValue":"banana"
},
{
"selectName":"kiwi",
"optionValue":"kiwi"
}
]
}
这个。相反,我想解析整个parse
JSON
答案 0 :(得分:1)
您可以将true作为第二个参数传递给json_decode,以将数据作为数组而不是对象返回。然后你可以做以下事情:
// 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>';
此外,我已经复制了您使用“选择名称”所做的事情。每个选项的值作为选择框选项值以及名称。不确定是否要更改它以使用&#39; optionValue&#39;价值相反,但我认为我提出它以防万一。
希望这有帮助!
答案 1 :(得分:1)
你的代码中有一些错误,如:
echo '<select name=''>';
应该是:
echo '<select name="">';
您可以将对象转换为数组并解析它:
<?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>';
答案 2 :(得分:1)
你有一些带引号的错误,如果它不是空的你需要在你的嵌套json中迭代
$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>
如果您不知道属性名称,因为它会随时更改
$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>';
}
}
}
}
答案 3 :(得分: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>';