Array problemer
Jeg skal have nogle xml data over i et array.min kode til dette ser sådan ud:
--------------------
function XmlToArray()
{
$file_url = $this->XmlConnectionUrl(0);
$xmlFileData = file_get_contents($file_url);
$xmlData = new SimpleXMLElement($xmlFileData);
foreach($xmlData->questions->question as $question)
{
foreach($question->answers->answer as $answer)
{
if($answer->attributes()->uid == 1153)
{
$arrayAnswer[] = $answer;
}
}
$countries[] = array($question->question_title,$question->question_text,$arrayAnswer);
}
return $countries;
}
-------------
udsnit af xml:
<?xml version="1.0" encoding="UTF-8" ?>
- <survey>
- <questions>
- <question>
<question_title>XXX</question_title>
<question_type>MetricQuestionSet</question_type>
- <question_text>
- <![CDATA[
1. 1 XXXX
]]>
</question_text>
- <answers>
<answer uid="1153" value="15,0">I knew nothing</answer>
<answer uid="1153" value="30,0">I knew little</answer>
<answer uid="1153" value="25,0">I knew some</answer>
<answer uid="1153" value="25,0">I knew a lot</answer>
<answer uid="1153" value="5,0">Don't know / do not wish to answer</answer>
<answer uid="1269" value="10,0">I knew nothing</answer>
<answer uid="1269" value="20,0">I knew little</answer>
<answer uid="1269" value="30,0">I knew some</answer>
<answer uid="1269" value="20,0">I knew a lot</answer>
<answer uid="1269" value="20,0">Don't know / do not wish to answer</answer>
</answers>
</question>
- <question>
<question_title>XXX</question_title>
<question_type>MetricQuestionSet</question_type>
- <question_text>
- <![CDATA[
1. 2 XXX
]]>
</question_text>
- <answers>
<answer uid="1153" value="45,0">Very concerned</answer>
<answer uid="1153" value="15,0">Fairly concerned</answer>
<answer uid="1153" value="30,0">Slightly concerned</answer>
<answer uid="1153" value="10,0">Not concerned</answer>
<answer uid="1153" value="0,0">Don't know / do not wish to answer</answer>
<answer uid="1269" value="40,0">Very concerned</answer>
<answer uid="1269" value="10,0">Fairly concerned</answer>
<answer uid="1269" value="10,0">Slightly concerned</answer>
<answer uid="1269" value="30,0">Not concerned</answer>
<answer uid="1269" value="10,0">Don't know / do not wish to answer</answer>
</answers>
</question>
</questions>
</survey>
--------
Mit problem er at $arrayAnswer kommer til at indeholde svarene fra både spørgsmål 1 og spørgsmål 2.
Jeg vil gerne lave det sådan at $countries[0][2] kun indeholder svarmulighederne knyttet til spørgsmål 1 og $countries[1][2] fra spørgsmål 2 osv.
Hvordan løser jeg dette?