php - How do I parse ul li a tree into a nested array using simpleXML?

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:

<ul>
   <li>
      <a>Order</a>
      <ul>
         <li>
            <a>Id</a>
            <ul>
               <li>13390</li>
            </ul>
         </li>
         <li>
            <a>OrderNumber</a>
            <ul>
               <li>13390</li>
            </ul>
         </li>
<li>
            <a>LineItems</a>
            <ul>
               <li>
                  <a>LineItem</a>
                  <ul>
                     <li>
                        <a>Id</a>
                        <ul>
                           <li>21598</li>
                        </ul>
                     </li>
                     <li>
                        <a>Subtotal</a>
                        <ul>
                           <li>45.79</li>
                        </ul>
                     </li>
                     <li>
                        <a>SubtotalTax</a>
                        <ul>
                           <li>9.16</li>
                        </ul>
                     </li>
                     <li>
                        <a>Total</a>
                        <ul>
                           <li>45.79</li>
                        </ul>
                     </li>
                     <li>
                        <a>TotalTax</a>
                        <ul>
                           <li>9.16</li>
                        </ul>
                     </li>
                     <li>
                        <a>Price</a>
                        <ul>
                           <li>45.79</li>
                        </ul>
                     </li>
                     <li>
                        <a>Quantity</a>
                        <ul>
                           <li>1</li>
                        </ul>
                     </li>
                     <li><a>TaxClass</a></li>
                     <li>
                        <a>Name</a>
                        <ul>
                           <li>Product Name</li>
                        </ul>
                     </li>
                     <li>
                        <a>ProductId</a>
                        <ul>
                           <li>4208</li>
                        </ul>
                     </li>
                     <li>
                        <a>Sku</a>
                        <ul>
                           <li>ABCD-S</li>
                        </ul>
                     </li>
                     <li><a>Meta</a></li>
                  </ul>
               </li>
            </ul>
         </li>
      </ul>
   </li>
</ul>

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.

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.

So far I've got the following recursive PHP function:

function ul_to_array ($ul) {
  if (is_string($ul)) {
    // encode ampersand appropiately to avoid parsing warnings
    $ul=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $ul);
    if (!$ul = simplexml_load_string($ul)) {
      trigger_error("Syntax error in UL/LI structure");
      return FALSE;
    }
    return ul_to_array($ul);
  } else if (is_object($ul)) {
    $output = array();
    foreach ($ul->li as $li) {
        $output[] = (isset($li->ul)) ? ul_to_array($li->ul) : (string) $li;
    }
    return $output;
  } else return FALSE;
}

But it outputs an array like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 13390
                )

            [1] => Array
                (
                    [0] => 13390
                )
            [2] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 21598
                                )

                            [1] => Array
                                (
                                    [0] => 45.79
                                )

                            [2] => Array
                                (
                                    [0] => 9.16
                                )

                            [3] => Array
                                (
                                    [0] => 45.79
                                )

                            [4] => Array
                                (
                                    [0] => 9.16
                                )

                            [5] => Array
                                (
                                    [0] => 45.79
                                )

                            [6] => Array
                                (
                                    [0] => 1
                                )

                            [7] => 
                            [8] => Array
                                (
                                    [0] => Product Name
                                )

                            [9] => Array
                                (
                                    [0] => 4208
                                )

                            [10] => Array
                                (
                                    [0] => ABCD-S
                                )

                            [11] => 
                        )

                )

My desired outcome is for the array to look something like this:

Array
(
    ["Order"] => Array
        (
            ["Id"] => 13390

            ["OrderNumber"] => 13390

            ["LineItems"] => Array
                (
                    [0] => Array
                        (
                            ["Id"] => 21598

                            ["Subtotal"] => 45.79

                            ["SubtotalTax"] =>  9.16

                            ["Total"] => 45.79

                            ["TotalTax"] => 9.16

                            ["Price"] => 45.79

                            ["Quantity"] => 1

                            ["TaxClass"] => 
                            ["Name"] => Product Name

                            ["ProductId"] => 4208

                            ["Sku"] => ABCD-S

                            ["Meta"] => 
                        )

                )

        )
)

I have tried replacing $output[] = with $output[$li->a] = when $li->a isn't empty but I just end up with errors and an empty array.

Any help would be greatly appreciated,

Thanks,

Answer

Solution:

The main change here is only to the loop which processes the XML...

    foreach ($ul->li as $li) {
        $newLayer = (isset($li->ul)) ? ul_to_array($li->ul) : trim((string) $li);
        $output[ (string)$li->a ] = $newLayer[''] ?? $newLayer;
    }

The (string)$li->a part uses the value from the anchor tag as the index of the array.

The $newLayer[''] ?? $newLayer processes the instances where the actual values are stored, so the value is in element '', if this element is not set then it will be a sub menu.

With the example you have, this gives...

Array
(
    [Order] => Array
        (
            [Id] => 13390
            [OrderNumber] => 13390
            [LineItems] => Array
                (
                    [LineItem] => Array
                        (
                            [Id] => 21598
                            [Subtotal] => 45.79
                            [SubtotalTax] => 9.16
                            [Total] => 45.79
                            [TotalTax] => 9.16
                            [Price] => 45.79
                            [Quantity] => 1
                            [TaxClass] => 
                            [Name] => Product Name
                            [ProductId] => 4208
                            [Sku] => ABCD-S
                            [Meta] => 
                        )

                )

        )

)

Source