title: PHP foreach iterating too often on SimpleXML object
body: <p>I'm using simpleXML to parse a xml file and want to iterate through the elements using a foreach loop and store it to a mysql database with help of a Yii model.</p>
  
  <p>I have a sample xml file containing 1083 entries each having three attributes. When using PHP version 5.3.1 on Windows it creates 1208 entries in the database table instead of 1083. I tried to find the problem in my code but after hours of testing I decided to switch to PHP version 5.4.7 on Windows. In this version it creates 1083 entries which is correct. But I want to find out what the problem exactly is because I need to find a solution because on my productive environment on a linux server I have PHP version 5.4.15-1 which also creates a wrong number of entries.</p>
  
  <p>That's some example xml:</p>
  
  <pre><code>&lt;listentries&gt;
    &lt;listentry&gt;
      &lt;name&gt;downs&lt;/name&gt;
      &lt;value&gt;-1.5&lt;/value&gt;
      &lt;type&gt;STATIC&lt;/type&gt;
    &lt;/listentry&gt;
    &lt;listentry&gt;
      &lt;name&gt;upcoming&lt;/name&gt;
      &lt;value&gt;1.0&lt;/value&gt;
      &lt;type&gt;STATIC&lt;/type&gt;
    &lt;/listentry&gt;
    ...
  &lt;/listentries&gt;
  </code></pre>
  
  <p>So my problem is that the foreach loop is iterating too often.</p>
  
  <pre><code>$list = simplexml_load_file($filename);
  foreach($list-&gt;listentries-&gt;listentry as $entry){
       $model = new Entry;
       $model-&gt;name = $entry-&gt;name;
       $model-&gt;value = $entry-&gt;value;
       $model-&gt;type = $entry-&gt;type;
       $model-&gt;save();
  }
  </code></pre>
  
  <p>When echoing the number of list elements I get 1083. When using a counter for counting the iterations I get 1208. So something very weird is going on. The 1208 inserted entries in the database looks the following: each entry is inserted at least once but some are inserted twice.</p>
  
  <p>Does anybody have an idea where the missbehavior is coming from exactly?</p>
  
  <p>I found out that the correct number of iterations is done when the xml file contains less than 970 entries.</p>
  
  <p>My configurations and number of inserted entries after importing xml containing 1083 entries:</p>
  
  <ul>
  <li>Windows, PHP 5.3.1, libxml 2.7.6: 1208 entries</li>
  <li>Windows, PHP 5.4.7, libxml 2.7.8: 1083 entries</li>
  <li>Linux, PHP 5.4.15-1, libxml 2.7.6: 1208 entries</li>
  </ul>
  
  <p>So it looks like updating libxml could help!?</p>