Clitorio Clitorio - 2 months ago 8
PHP Question

PHP foreach stop after first child is met

Please keep in ming all read a lot of "related" answer here and used Google a lot before coming to you.

It's my first hand at xml using PHP code.

Here is the xml file (test.xml) I'm looking at:

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


And the PHP code :

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


My goal is to get all ID questions (to later put them in database)

The foreach seems to see only go through the first child of the xml document (on which I don't have any control on the structure) and stop. When I execute it, I only get the first question ID.

I'm expecting the following result :

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


And I get :

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


What am I missing?

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

Because you only have one question node, then you're only going to get one iteration in a loop.

You're getting the first value because you actually dig in to that first node.

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

So basically, just go two nodes deep for your loop, and then only level deep in the loop.

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

This outputs:

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