2016-09-14 10 views
1

ここで、「関連する」回答をたくさん読んで、あなたに来る前にGoogleをたくさん使ってください。最初の子供が満たされた後にPHP foreach stop

これはPHPコードを使用したxmlの私の最初の手です。

<quizReport xmlns="http://empty.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://empty.com" version="1"> 
<quizSettings quizType="graded" maxScore="460" maxNormalizedScore="100" timeLimit="0"><passingPercent>0.6</passingPercent></quizSettings> 
<summary score="142" percent="0.3087" time="192"><variables><variable name="USER_NAME" title="Nom" value="test1457"/><variable name="USER_EMAIL" title="Courriel" value="[email protected]"/><variable name="DEPARTEMENT" title="Departement" value="Designers"/></variables></summary> 
<questions> 
<multipleChoiceQuestion id="{3BFC44A3-137B-496B-900D-E6908253C106}" status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1"><direction>Combien de temps cela prend-il, en moyenne, pour concevoir et produire une ouverture en 3D pour un bulletin d'actualités? 
    </direction><answers correctAnswerIndex="2" userAnswerIndex="2"><answer>6 jours 
    </answer><answer>24 jours 
    </answer><answer>6 semaines 
    </answer><answer>24 semaines 
    </answer></answers></multipleChoiceQuestion> 

    <multipleChoiceQuestion id="{2CEDBF79-0864-4E1A-954A-F7CA17989314}" status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1"><direction>Combien d'appels par heure sont traités par l'équipe du CCR et des moyens de production en période de pointe régulière (sans événements spéciaux)? 
    </direction><answers correctAnswerIndex="2" userAnswerIndex="2"><answer>Environ 15 appels par heure 
    </answer><answer>Environ 25 appels par heure 
    </answer><answer>Environ 35 appels par heure 
    </answer><answer>Environ 65 appels par heure 
    </answer></answers></multipleChoiceQuestion> 

    <multipleChoiceQuestion id="{71221928-8909-44BC-94B4-C0C011E297F7}" status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1"><direction>Avec qui les médiamans travaillent-ils de concert pour déterminer ce qui doit être conservé dans le système d'archivage? 
    </direction><answers correctAnswerIndex="0" userAnswerIndex="0"><answer>Les médiathécaires 
    </answer><answer>Les monteurs 
    </answer><answer>Les directeurs techniques 
    </answer><answer>Les chefs de pupitre 
    </answer></answers></multipleChoiceQuestion> 
    <multipleChoiceQuestion id="{C6CDB118-B4E0-4856-A7AB-69A35CEBBB44}" status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1"><direction>Quelle est la capacité du système de stockage robotisé sur lequel les médiamans envoient les archives du système Avid? 
    </direction><answers correctAnswerIndex="3" userAnswerIndex="3"><answer>500 Gigaoctets 
    </answer><answer>12 Téraoctects 
    </answer><answer>500 Téraoctects 
    </answer><answer>12 Pétaoctets 
    </answer></answers></multipleChoiceQuestion> 
</questions> 
</quizReport> 

そしてPHPコード:ここで

は私が探していたXMLファイル(のtest.xml)である

<?php 

error_reporting(E_ALL); ini_set('display_errors', 1); 

$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object"); 

foreach($xml->questions as $multipleChoiceQuestion) { 
    echo $multipleChoiceQuestion->multipleChoiceQuestion['id']; 
    echo "<br>"; 
} 
?> 

私の目標は、後に(すべてのIDの質問を取得することです

foreachは、XML文書の最初の子(構造上は何も制御していない)だけを見て、停止しているようです。私はそれを実行するとき、私は最初の質問IDを取得します。

{3BFC44A3-137B-496B-900D-E6908253C106} 
{2CEDBF79-0864-4E1A-954A-F7CA17989314} 
{71221928-8909-44BC-94B4-C0C011E297F7} 
{C6CDB118-B4E0-4856-A7AB-69A35CEBBB44} 

そして私が手::

{3BFC44A3-137B-496B-900D-E6908253C106} 

私は何をしないのです

私は、次のような結果を期待していますか?

+0

これは、[有効なXML](http://xmlvalidation.com/)ではないので、あなたは、いくつかのコピー/貼り付けの問題を持っています。閉じる 'summary'タグがありません。 [MCVE](http://stackoverflow.com/help/mcve)を作成してください。 –

+0

申し訳ありませんが何らかの理由で、終了タグがありませんでした。 I'va made enを編集しました。現在提供されているコードは、私が持っているオリジナルと比較されます。 – Clitorio

答えて

0
foreach($xml->questions as $multipleChoiceQuestion) 

一つだけquestionノードを持っているので、その後、あなただけのループに一回の反復を取得するつもりです。

実際にその最初のノードを掘り下げているため、最初の値が得られます。

echo $multipleChoiceQuestion->multipleChoiceQuestion['id']; 

基本的に、2つのノードをループの深度に移動してから、ループの深さを深くしてください。

<?php 

error_reporting(E_ALL); ini_set('display_errors', 1); 

$xml=simplexml_load_file(__DIR__."/test.xml") or die("Error: Cannot create object"); 

foreach($xml->questions->multipleChoiceQuestion as $multipleChoiceQuestion) { 
    echo $multipleChoiceQuestion['id']; 
    echo "<br>"; 
} 

この出力:

{3BFC44A3-137B-496B-900D-E6908253C106}
{2CEDBF79-0864-4E1A-954A-F7CA17989314}
{71221928-8909-44BC-94B4-C0C011E297F7 } {
C6CDB118-B4E0-4856-A7AB-69A35CEBBB44}

+0

それはトリックでした!解決済み! 私はそれが何かばかげたと知っていた...;) – Clitorio

関連する問題