title: Node No Longer Exists in SimpleXML
body: <p>I have a problem that I cannot seem to fix.  The code below will return a php error "<em>Node no longer exists</em>" when <code>$array</code> is empty.  If <code>$array</code> is not empty it works fine. The error will show up for the line with <code>$prinid = $array[0];</code> when <code>$array</code> is empty.</p>
  
  <pre><code>$doc = new SimpleXmlElement($data, LIBXML_NOCDATA); 
  
  foreach($doc as $a =&gt; $b) {
      if ($a == 'principal-list') {
          $array = $b-&gt;principal-&gt;attributes();
      }
  }
  
  $prinid = $array[0];
  
  if (isset($array[0])) {
      $currentuser = 1;
  } else {
      $currentuser = 0;
  }
  </code></pre>
  
  <h3>Update:</h3>
  
  <p>Here is what I have now and I get:</p>
  
  <blockquote>
    <p>Warning: count() [function.count]: Node no longer exists in * * * * * * *</p>
  </blockquote>
  
  <pre><code>$doc = new SimpleXmlElement($data, LIBXML_NOCDATA); 
  
  foreach($doc as $a =&gt; $b) {
      if ($a == 'principal-list') {
          $array = $b-&gt;principal-&gt;attributes();
      }
  }
  
  $currentuser = 0;
  if (isset($array) &amp;&amp; count($array) &gt; 0) {
      $prinid = $array[0];
      $currentuser = 1;
  }
  </code></pre>