I'm getting grades from a gradebook in GradPoint using their DLAP API (we have an in-house Student Info System at my school that I built). It's a bit of a nightmare because their arrays are nested many times over. Here's the output array I need to retrieve data from (replacing some values with "REMOVED" because student data):
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
)
)
)
)
)
)
)
)
I'm trying to get the [grade] section. Specifically, the data inside @attributes that's inside of [grades]. Here's my foreach code:
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;
}
}
}
}
}
}
}
}
}
}
My Question: Is my giant foreach statement really the best way to retrieve this data? Is there a function that will iterate through all that and give me just what I want?
What I need to do is input those values as a line item in a MySQL table, as an fyi, that's why I saved those values in variables. This giant foreach will work, so I'm not asking for handouts. But there must be a better way!
Thanks in advanced.
arrray_walk_recursiveis what you need php.net/manual/en/function.array-walk-recursive.php