美好的一天,
我直接从API端点获取数据。我能够获得id
,但我如何才能"type":"participant",
专门访问skillTier
?
这是我的代码,但它引发了错误:NOTICE: UNDEFINED INDEX: SKILLTIER IN C:\XAMPP\HTDOCS\VGAPP\INDEX.PHP ON LINE 219
if(is_array($finaljson) || is_object($finaljson)){
if(!empty($finaljson['included']) && $finaljson['included'] !== ""){
foreach ($finaljson['included'] as $key => $value) {
if(!empty($value['attributes']['name']) && $value['attributes']['name'] === $username){
$userid = $value['id'];
$skillTier = $value['skillTier'];
}
}
这是json数据:
https://gist.github.com/kamaelxiii/18ef0c1600330b98717b96db00532d6a
答案 0 :(得分:1)
与访问var refcur refcursor
exec p_matrika(2010, 2.5, '26311330', :refcur);
print refcur
的方式相同,它是多维数组,因此将其视为一个城镇,并且想要到达9号楼的6号楼,这是位于4号街道,如果没有告知完整地址,您将无法要求有人告诉您6号楼的位置,所以在您的情况下,只有在您提供变量时才能获得$value['attributes']['name']
索引索引的完整地址,如下所示:
skillTier
答案 1 :(得分:1)
foreach($finaljson['included'] as $included) {
if($included['type'] == 'participant'){
$userid = $included['id'];
$skillTier = $included['attributes']['stats']['skillTier'];
}
}
答案 2 :(得分:1)
首先,您有两个PHP语法错误,其中有两个}
{}}关闭了您的第一个if
语句和foreach
循环。
此外,您还想循环included
个节点,并在继续之前检查您是否有participant
个对象,因为其他项目不具备skillTier值。在尝试将其保存在变量中之前检查技能人员是否为空的最佳做法。
这是您的代码的更新版本:
if(is_array($finaljson) || is_object($finaljson)) {
foreach($finaljson['included'] as $included) {
if($included['type'] === 'participant') {
if(!empty($included['attributes']['stats']['skillTier'])){
$id = $included['id'];
$skillTier = $included['attributes']['stats']['skillTier'];
}
}
}
}
这是一个使用您的JSON的工作示例,它将使用var_dump ID和技能人员。 http://ideone.com/DrXWe7(点击fork查看代码)
答案 3 :(得分:1)
将一个大型json /阵列缩小到目标并隔离所需数据是一项耐心的工作。您只需要逐步遍历结构,将每个级别的数据打印到屏幕,并识别保存所需内容的键。
代码:
$array = json_decode($json, true);
foreach ($array['included'] as $included) { // iterate all children of "included"
if ($included['type'] === 'participant') { // only access "participant" subset data
$result[$included['id']] = $included['attributes']['stats']['skillTier']; // access the skillTier data which is beneath "stats" beneath "attributes"
}
}
echo "<pre>";
var_export($result);
echo "</pre>";
输出:
array (
'4cfe4736-f47b-11e6-b633-0242ac110030' => 16,
'4cfe473f-f47b-11e6-b633-0242ac110030' => 10,
'4cfe4727-f47b-11e6-b633-0242ac110030' => 27,
'd4003883-f423-11e6-865e-0242ac110008' => 23,
'd4003869-f423-11e6-865e-0242ac110008' => 24,
'd400387a-f423-11e6-865e-0242ac110008' => 23,
'4cfe471f-f47b-11e6-b633-0242ac110030' => 27,
'd400386d-f423-11e6-865e-0242ac110008' => 24,
'd4003887-f423-11e6-865e-0242ac110008' => 23,
'4cfe4724-f47b-11e6-b633-0242ac110030' => 24,
'd4003864-f423-11e6-865e-0242ac110008' => 18,
'4cfe473b-f47b-11e6-b633-0242ac110030' => 17,
)