再会,
我直接从 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
减少一个大的 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,
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句