У меня такой json. Не понимаю, как анализировать и отображать внутри поля выбора, используя php под заголовком name.Basically У этого есть 2 имени Клиенты, Lead.So каждый из значений будет отображаться под этим заголовком. Первый шаг
$data = json_decode($json);
и результаты будут представлены? но как показать названия голов?
{
"status": {
"message": "success",
"code": 200
},
"clients": [
{
"values": [
{
"Code": "rdf",
"name": "Adfgf"
},
{
"Code": "fg",
"name": "fg"
}
],
"name": "Customers"
},
{
"values": [
{
"Code": "fgf",
"name": "fgdf"
},
{
"Code": "api",
"name": "fgfdgd"
},
{
"Code": "fgbb",
"name": "mnn"
},
{
"Code": "acy",
"name": "System"
}
],
"name": "Lead"
}
]
}
Я исправил код http://codepad.org/vLhzs6kJ#output
$string = '{
"status": {
"message": "success",
"code": 200
},
"clients": [
{
"values": [
{
"Code": "rdf",
"name": "first"
},
{
"Code": "fg",
"name": "second"
}
],
"name": "Customers"
},
{
"values": [
{
"Code": "fgf",
"name": "third"
},
{
"Code": "api",
"name": "4th"
},
{
"Code": "fgbb",
"name": "5th"
},
{
"Code": "acy",
"name": "last"
}
],
"name": "Lead"
}
]
}';
$data = json_decode($string,true);
echo "<Select>";
foreach ($data['clients'] as $c) {
$gp =$c['name'];
echo "<optgroup label=$gp>";
# calling the function on the values data
recurse($c['values']);
echo '</optgroup>';
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['Code'] . '">'
. $n['name']
. '</option>'
;
}
}
echo "</Select>";