I am stuck with getting the value from an XML element list. I wonder if someone here could help?
Here is the problem. I have this XML element:
<sequenceQuestion status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1">
<direction>Order the following options</direction>
<answers>
<answer userDefinedPosition="0">Option 1</answer>
<answer userDefinedPosition="1">Option 2</answer>
<answer userDefinedPosition="2">Option 3</answer>
<answer userDefinedPosition="3">Option 4</answer>
<answer userDefinedPosition="4">Option 5</answer>
</answers>
</sequenceQuestion>
I need to print the elements in the tag. For example, I need to print "Option 1", "Option 2", ... , "Option 5" in a webpage by the order they were selected by the user.
If I use this code:
$SequenceQuestionNumber = 1;
foreach($answer_group->answers->answer as $userSequenceQuestion){
if($status!="notAnswered" && isset($userSequenceQuestion->attributes()->userDefinedPosition)) {
$userDefinedPosition = (string)$userSequenceQuestion->attributes()->userDefinedPosition;
$values .= "\"" . $userDefinedPosition . "\", ";
I get as answer values "0", "1", "2", "3" and "4". But what I need is not the 'userDefinedPosition" but rather the text associated with it. For "0" I need to print "Option 1", for "1" I need "Option 2", for "3" I need "Option 2", etc...
I have tried to build an array like this:
$answers = (array)$userSequenceQuestion->answer;
$values .= "\"" . $answers[$userDefinedPosition] . "\", ";
but I just get blanks (nothing) printed.
I guess what is messing my reference is the userDefinedPosition
inside the tag, but I am not sure on how to get the labels instead of the index numbers.