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]+;)/', '&', $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,
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] =>
)
)
)
)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.