我正在使用他们的DLAP API从GradPoint的成绩簿中获得成绩(我在学校建立了一个内部学生信息系统)。这是一个噩梦,因为他们的阵列嵌套了很多次。这是我需要从中检索数据的输出数组(用“REMOVED”替换一些值,因为学生数据):
Array
(
[@attributes] => Array
(
[code] => OK
)
[enrollments] => Array
(
[enrollment] => Array
(
[@attributes] => Array
(
[id] => REMOVED
[userid] => REMOVED
[entityid] => REMOVED
[roleid] => 0
[domainid] => REMOVED
[reference] => REMOVED
[guid] => REMOVED
[flags] => REMOVED
[status] => 1
[startdate] => 2015-07-30T06:00:00Z
[enddate] => 2015-12-17T06:59:00Z
)
[data] => Array
(
[status] => Array
(
[performance] => Array
(
[@attributes] => Array
(
[signal] => Red
[code] => 2
)
)
[pace] => Array
(
[@attributes] => Array
(
[signal] => Green
)
)
)
)
[user] => Array
(
[@attributes] => Array
(
[id] => REMOVED
[firstname] => REMOVED
[lastname] => REMOVED
[reference] => REMOVED
[guid] => REMOVED
[userspace] => REMOVED
[username] => REMOVED
[email] => REMOVED
[lastlogindate] => 2015-08-07T21:43:46.11Z
)
)
[domain] => Array
(
[@attributes] => Array
(
[id] => REMOVED
[name] => REMOVED
)
)
[grades] => Array
(
[@attributes] => Array
(
[achieved] => 13.5
[possible] => 100
[letter] => F
[passingscore] => 0.8
[complete] => 0.5
[seconds] => 8331
)
[categories] => Array
(
[category] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 15
[name] => Assignments
[achieved] => 27
[possible] => 30
[letter] => A
[seconds] => 5580
)
)
[1] => Array
(
[@attributes] => Array
(
[id] => 138
[name] => Assessments
[achieved] => 0
[possible] => 25
[letter] => F
[seconds] => 2760
)
)
)
)
)
)
)
)
我正试图获得[等级]部分。具体来说,@ grade中的数据位于[grade]内部。这是我的foreach代码:
foreach ($array_data as $key=>$value)
{
if($key == "enrollments")
{
foreach ($value as $key1=>$value1)
{
if($key1 == "enrollment")
{
foreach ($value1 as $key2=>$value2)
{
if($key2 == "grades")
{
foreach ($value2 as $key3=>$value3)
{
if($key3 == "@attributes")
{
foreach ($value3 as $key4=>$value4)
{
switch($key4)
{
case "achieved":
$gpAchieved = $value4;
break;
case "possible":
$gpPossible = $value4;
break;
case "letter":
$gpLetter = $value4;
break;
case "passingscore":
$gpPassingScore = $value4;
break;
case "complete":
$gpComplete = $value4;
break;
case "seconds":
$gpSeconds = $value4;
break;
}
}
}
}
}
}
}
}
}
}
我的问题:我的巨大foreach声明真的是检索这些数据的最佳方法吗?是否有一个函数可以遍历所有这些并给我我想要的东西?
我需要做的是将这些值作为一个行项输入MySQL表中,作为fyi,这就是我将这些值保存在变量中的原因。这个巨大的foreach会起作用,所以我不是要求施舍。但必须有更好的方法!
先谢谢。
答案 0 :(得分:1)
如果您的数据结构始终相同,则您不必总是遍历每个字段,只需直接转到所需的条目即可。
如果只返回1个注册,您可以将查找修复为以下内容:
if (isset($arr_test['enrollments']['enrollment']['grades']['@attributes'])) {
$arr_gradesAttributes = $arr_test['enrollments']['enrollment']['grades']['@attributes'];
$gpAchieved = $arr_gradesAttributes['achieved'];
$gpPossible = $arr_gradesAttributes['possible'];
$gpLetter = $arr_gradesAttributes['letter'];
$gpPassingScore = $arr_gradesAttributes['passingscore'];
$gpComplete = $arr_gradesAttributes['complete'];
$gpSeconds = $arr_gradesAttributes['seconds'];
}
print("\$gpAchieved={$gpAchieved}, \$gpPossible={$gpPossible}, \$gpLetter={$gpLetter}, \$gpPassingScore={$gpPassingScore}, \$gpComplete={$gpComplete}, \$gpSeconds={$gpSeconds}");