使用php解析2头json和分组
我有一个类似这样的json,但不知道如何使用php在名称标题下解析并显示在选择框中 命名客户,Lead.So每个值都将显示在这些标题下。 第一步是使用php解析2头json和分组,php,json,Php,Json,我有一个类似这样的json,但不知道如何使用php在名称标题下解析并显示在选择框中 命名客户,Lead.So每个值都将显示在这些标题下。 第一步是 $data = json_decode($json); 一个foreach会提供结果吗?但是如何显示头部名称呢 { "status": { "message": "success", "code": 200 }, "clients": [ { "valu
$data = json_decode($json);
一个foreach会提供结果吗?但是如何显示头部名称呢
{
"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"
}
]
}
我已经修好了密码
这不是有效的JSON:它是从webservice获取的,当我在chrome插件中检查REST客户端时,它的返回是这样的。你需要回显原始JSON并发布它。@AbraCadaver我已经更新了有效代码第一步是$data=JSON\u decode$JSON;那是一辆福特车吗?
$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>";