title: How to display xml data in recursive format to show true child/parent relationships from data
body: <p>I am attempting to display data an XML source, in a tree format, to display correct Parent -> Child relationship.</p>
  
  <p>The problem is the child nodes can have multiple child's themselves (there is no limit)</p>
  
  <p>An example of the output that works in flash = <a href="http://fluffyduck.com.au/example-layout.jpg" rel="nofollow">http://fluffyduck.com.au/example-layout.jpg</a></p>
  
  <p>I cannot replicate this in PHP / html however.
  I have provided an example XML source here : <a href="http://www.fluffyduck.com.au/sampleXML.txt" rel="nofollow">http://www.fluffyduck.com.au/sampleXML.txt</a></p>
  
  <p>With the help of some other people, a list of parent ID's and their children, as follows :</p>
  
  <pre><code>&lt;?php
  
  $content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt');
  $xml = new SimpleXMLElement($content);
  
  $users = array ();
  myUserReader($xml-&gt;user, $users);
  
  function myUserReader($node, &amp;$users)
  {
      if (array_key_exists("{$node['id']}", $users) === false)
      {
          $users["{$node['id']}"] = array ();
      }
      if (isset($node-&gt;user))
      {
          for ($key = 0; ($key &lt; count($node-&gt;user)); $key++)
          {
              $user = $node-&gt;user[$key];
              if (!in_array("{$node['id']}", $users["{$node['id']}"]))
              {
                  $users["{$node['id']}"][] = "{$user['id']}";
  #                $users["{$node['id']}"][] = $user;
              }
              myUserReader($user, $users);
          }
      }
  }
  
  foreach ($users as $parent =&gt; $children)
  {
      echo "{$parent}: " . implode(", ", $children) . "\n";
  }
  ?&gt;
  </code></pre>
  
  <p>I am unable to work out what code I need to parse the above results and display the information as per the attached image above.</p>
  
  <p>I have contemplated converting the XML to an array, but still have the same trouble, the code I have tried for that is :</p>
  
  <pre><code>function xmlstr_to_array($xmlstr) {
    $doc = new DOMDocument();
    $doc-&gt;loadXML($xmlstr);
    return domnode_to_array($doc-&gt;documentElement);
  }
  function domnode_to_array($node) {
    $output = array();
    switch ($node-&gt;nodeType) {
     case XML_CDATA_SECTION_NODE:
     case XML_TEXT_NODE:
      $output = trim($node-&gt;textContent);
     break;
     case XML_ELEMENT_NODE:
      for ($i=0, $m=$node-&gt;childNodes-&gt;length; $i&lt;$m; $i++) {
       $child = $node-&gt;childNodes-&gt;item($i);
       $v = domnode_to_array($child);
       if(isset($child-&gt;tagName)) {
         $t = $child-&gt;tagName;
         if(!isset($output[$t])) {
          $output[$t] = array();
         }
         $output[$t][] = $v;
       }
       elseif($v) {
        $output = (string) $v;
       }
      }
      if(is_array($output)) {
       if($node-&gt;attributes-&gt;length) {
        $a = array();
        foreach($node-&gt;attributes as $attrName =&gt; $attrNode) {
         $a[$attrName] = (string) $attrNode-&gt;value;
        }
        $output['@attributes'] = $a;
       }
       foreach ($output as $t =&gt; $v) {
        if(is_array($v) &amp;&amp; count($v)==1 &amp;&amp; $t!='@attributes') {
         $output[$t] = $v[0];
        }
       }
      }
     break;
    }
    return $output;
  }
  
  $moo = xmlstr_to_array($content);
  print_r($moo);
  </code></pre>
  
  <p>However the same problem exists, I do not know how to cycle through and display the information in the correct Parent -> Child manner recursively.</p>
  
  <p>Due to the way the data is constructed I also cannot work out how to retrieve a "single" entry out, if i could do that perhaps I could manually rebuild the tree, I'm not sure how as each record does not have a unique identifer.</p>
  
  <p>Any help greatly appreciated.</p>