2017-11-15 7 views
-2

私のスクリプトはXMLファイルを開き、すべてのタグ、子、子どもの子などをループして情報を吐き出すようになっています。今日私は子供をループするはずの私のforeachループが最後の子供に直接スキップしているという点で面白いバグに気付きました。XMLファイルの最後の子にスキップする子どものためのForeachループ

function theHunt($node) 
{ 
    $tagName = ''; 
    print 'I am starting with ' . $node->getName() . ' It should have ' . $node->count() . ' children. The first child should be: ' . $node->children()->getName() . '<br>'; 

    foreach ($node->children() as $child); 
    { 
     print $child->getName() . '<br>'; 
     if (isset($child)) 
     { 
      print 'I found: ' . $child->getName() . ' I\'ll see if it has kids' . '<br>'; 
      $this->theHunt($child); 
     } 
     else 
     { 
      print 'No kids here, I\'m going to stop digging.<br>'; 
     } 
     //Now that I am all the way down or working my way back up. I start gathering my information. 
     $tagName = $node -> getName(); 
     if($this->rootNode->$tagName[0] !== NULL) 
     { 
      foreach ($this->rootNode->$tagName[0]->attributes() as $a => $b) ; 
      { 
       //echo $a, '="', $b, "<br>"; 
      } 
     } 
     //print_r($node); 
     print'<br> I kicked out <br>'; 

    } 
} 

本当に奇妙な部分がラインということです:

print 'I am starting with ' . $node->getName() . ' It should have ' . $node->count() . ' children. The first child should be: ' . $node->children()->getName() . '<br>'; 

がすべて正しい情報を出力しているが、foreachループに私がドロップした瞬間は、私は非常に最後の子に右スキップします。

答えて

0

コードがツリー構造を誤って処理しています。以下の構造に従ってください:

function theHunt($node) 
{ 
    foreach ($node->children() as $child); 
    { 
     if (isset($child)) 
     { //has children 
      theHunt($child); //go one level down 
     } 
     else 
     { //this is a child 
      //enum thru its attributes 
     } 
    } 
} 
+0

これは何もしませんでした。どうして私が木を誤って扱っているのか説明できますか?たぶん私は私がどのようにそれを修正することができるか知っている場合。 – CBrowning0791

-1

わかりました。本当に新人ミス。私はforeachの後にセミコロンを入れます。私は美しく走っています。

関連する問題