2016-09-14 4 views
1

여기에 많은 "관련"답변을 모두 남기고, Google에 오기 전에 많이 사용하십시오.첫 아이를 만난 후 PHP foreach 중지

PHP 코드를 사용하여 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 질문을 얻을 수 있습니다 :

<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의)입니다 데이터베이스에 넣으십시오.)

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

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}

+0

그 트릭을 했어! 해결! 나는 그것이 어리석은 무엇인가 알고 있었다. ...;) – Clitorio