title: How do I parse ul li a tree into a nested array using simpleXML?
body: <p>I'm working on a PHP script that requests xml data re: a customer order from a third party API (order management system) and since the main API call doesn't work properly (and they have no intention of fixing it either), the only way I can get the data I need is using a call that returns it as a jstree (html: ul li a) in the following format:</p>
  <pre><code>&lt;ul&gt;
     &lt;li&gt;
        &lt;a&gt;Order&lt;/a&gt;
        &lt;ul&gt;
           &lt;li&gt;
              &lt;a&gt;Id&lt;/a&gt;
              &lt;ul&gt;
                 &lt;li&gt;13390&lt;/li&gt;
              &lt;/ul&gt;
           &lt;/li&gt;
           &lt;li&gt;
              &lt;a&gt;OrderNumber&lt;/a&gt;
              &lt;ul&gt;
                 &lt;li&gt;13390&lt;/li&gt;
              &lt;/ul&gt;
           &lt;/li&gt;
  &lt;li&gt;
              &lt;a&gt;LineItems&lt;/a&gt;
              &lt;ul&gt;
                 &lt;li&gt;
                    &lt;a&gt;LineItem&lt;/a&gt;
                    &lt;ul&gt;
                       &lt;li&gt;
                          &lt;a&gt;Id&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;21598&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Subtotal&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;45.79&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;SubtotalTax&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;9.16&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Total&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;45.79&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;TotalTax&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;9.16&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Price&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;45.79&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Quantity&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;1&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;&lt;a&gt;TaxClass&lt;/a&gt;&lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Name&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;Product Name&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;ProductId&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;4208&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;
                          &lt;a&gt;Sku&lt;/a&gt;
                          &lt;ul&gt;
                             &lt;li&gt;ABCD-S&lt;/li&gt;
                          &lt;/ul&gt;
                       &lt;/li&gt;
                       &lt;li&gt;&lt;a&gt;Meta&lt;/a&gt;&lt;/li&gt;
                    &lt;/ul&gt;
                 &lt;/li&gt;
              &lt;/ul&gt;
           &lt;/li&gt;
        &lt;/ul&gt;
     &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  <p>I need to parse all of the above string into a nested array so I can foreach through the order items and utilising the values inside, carry out certain actions. I have omitted a large amount of the data tree from the above to keep things as simple as possible.</p>
  <p>I've tried SimpleHTMLDom already but it caused various memory and server errors as I developed the script further and scaled the amount of operations so have opted for SimpleXML, however still open to other options too to get the job done.</p>
  <p>So far I've got the following recursive PHP function:</p>
  <pre><code>function ul_to_array ($ul) {
    if (is_string($ul)) {
      // encode ampersand appropiately to avoid parsing warnings
      $ul=preg_replace('/&amp;(?!#?[a-z0-9]+;)/', '&amp;amp;', $ul);
      if (!$ul = simplexml_load_string($ul)) {
        trigger_error(&quot;Syntax error in UL/LI structure&quot;);
        return FALSE;
      }
      return ul_to_array($ul);
    } else if (is_object($ul)) {
      $output = array();
      foreach ($ul-&gt;li as $li) {
          $output[] = (isset($li-&gt;ul)) ? ul_to_array($li-&gt;ul) : (string) $li;
      }
      return $output;
    } else return FALSE;
  }
  </code></pre>
  <p>But it outputs an array like this:</p>
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; 13390
                  )
  
              [1] =&gt; Array
                  (
                      [0] =&gt; 13390
                  )
              [2] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [0] =&gt; Array
                                  (
                                      [0] =&gt; 21598
                                  )
  
                              [1] =&gt; Array
                                  (
                                      [0] =&gt; 45.79
                                  )
  
                              [2] =&gt; Array
                                  (
                                      [0] =&gt; 9.16
                                  )
  
                              [3] =&gt; Array
                                  (
                                      [0] =&gt; 45.79
                                  )
  
                              [4] =&gt; Array
                                  (
                                      [0] =&gt; 9.16
                                  )
  
                              [5] =&gt; Array
                                  (
                                      [0] =&gt; 45.79
                                  )
  
                              [6] =&gt; Array
                                  (
                                      [0] =&gt; 1
                                  )
  
                              [7] =&gt; 
                              [8] =&gt; Array
                                  (
                                      [0] =&gt; Product Name
                                  )
  
                              [9] =&gt; Array
                                  (
                                      [0] =&gt; 4208
                                  )
  
                              [10] =&gt; Array
                                  (
                                      [0] =&gt; ABCD-S
                                  )
  
                              [11] =&gt; 
                          )
  
                  )
  </code></pre>
  <p>My desired outcome is for the array to look something like this:</p>
  <pre><code>Array
  (
      [&quot;Order&quot;] =&gt; Array
          (
              [&quot;Id&quot;] =&gt; 13390
  
              [&quot;OrderNumber&quot;] =&gt; 13390
  
              [&quot;LineItems&quot;] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [&quot;Id&quot;] =&gt; 21598
  
                              [&quot;Subtotal&quot;] =&gt; 45.79
  
                              [&quot;SubtotalTax&quot;] =&gt;  9.16
  
                              [&quot;Total&quot;] =&gt; 45.79
  
                              [&quot;TotalTax&quot;] =&gt; 9.16
  
                              [&quot;Price&quot;] =&gt; 45.79
  
                              [&quot;Quantity&quot;] =&gt; 1
  
                              [&quot;TaxClass&quot;] =&gt; 
                              [&quot;Name&quot;] =&gt; Product Name
  
                              [&quot;ProductId&quot;] =&gt; 4208
  
                              [&quot;Sku&quot;] =&gt; ABCD-S
  
                              [&quot;Meta&quot;] =&gt; 
                          )
  
                  )
  
          )
  )
  </code></pre>
  <p>I have tried replacing <code>$output[] = </code> with <code>$output[$li-&gt;a] = </code> when $li-&gt;a isn't empty but I just end up with errors and an empty array.</p>
  <p>Any help would be greatly appreciated,</p>
  <p>Thanks,</p>