PHP foreach 在满足第一个 child 后停止

PHP foreach stop after first child is met

请记住,在来找你之前,请大家阅读这里的大量 "related" 答案并使用 Google 很多。

这是我使用 PHP 代码 xml 的第一手资料。

这是我正在查看的 xml 文件 (test.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="test@1457.com"/><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 代码:

<?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 文档的第一个 child(我对它的结构没有任何控制)并停止。当我执行它时,我只得到第一个问题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}

我错过了什么?

foreach($xml->questions as $multipleChoiceQuestion)

因为你只有一个 question 节点,所以你只会在一个循环中进行一次迭代。

您获得了第一个值,因为您实际上深入到第一个节点。

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

所以基本上,循环只需深入两个节点,然后在循环中只深入一层。

<?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}