{"id":12350,"date":"2024-08-16T10:45:00","date_gmt":"2024-08-16T09:45:00","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=12350"},"modified":"2024-08-15T11:47:00","modified_gmt":"2024-08-15T10:47:00","slug":"sorting-xml-data-in-php","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/sorting-xml-data-in-php\/","title":{"rendered":"Sorting XML data in PHP"},"content":{"rendered":"\n<p>There are plenty of different ways of sorting XML in PHP, but for me this is the easiest way.<\/p>\n\n\n\n<p>Take the sample XML file below which contains multiple vehicles.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;\n&lt;LIST&gt;\n  &lt;VEHICLE&gt;\n    &lt;YEAR&gt;2003&lt;\/YEAR&gt;\n    &lt;MAKE&gt;FORD&lt;\/MAKE&gt;\n    &lt;MODEL&gt;FOCUS&lt;\/MODEL&gt;\n  &lt;\/VEHICLE&gt;\n  &lt;VEHICLE&gt;\n    &lt;YEAR&gt;2004&lt;\/YEAR&gt;\n    &lt;MAKE&gt;AUDI&lt;\/MAKE&gt;\n    &lt;MODEL&gt;A3&lt;\/MODEL&gt;\n  &lt;\/VEHICLE&gt;\n  ...\n&lt;\/LIST<\/code><\/pre><\/div>\n\n\n\n<p>You could load the XML and loop through it to create an array using a FOREACH loop but it is easier and probably quicker to encode the data as JSON and then decode it which will produce an array in a single line of code without the worry of naming the elements in the array:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$xml \t= simplexml_load_file(&quot;cars.xml&quot;);\n$items \t= $xml-&gt;xpath(&quot;LIST\/VEHICLE&quot;);\n$cars   = json_decode(json_encode($items),true);<\/code><\/pre><\/div>\n\n\n\n<p>Then all you have to do is sort the array specifying the field to sort and the type of sort:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>array_multisort(array_column($cars,&#39;YEAR&#39;),SORT_ASC,SORT_NUMERIC,$cars);<\/code><\/pre><\/div>\n\n\n\n<p>Finally the last step is a simple output to screen:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>foreach($cars as $i)\n\techo &quot;&lt;p&gt;&quot;.$i[&quot;YEAR&quot;].&quot; - &quot;.&quot;.$i[&quot;MAKE&quot;].&quot; - &quot;.$i[&quot;MODEL&quot;].&quot;&lt;\/p&gt;&quot;;<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are plenty of different ways of sorting XML in PHP, but for me this is the easiest way. Take the sample XML file below which contains multiple vehicles. You could load the XML and loop through it to create an array using a FOREACH loop but it is easier and probably quicker to encode [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12354,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-12350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=12350"}],"version-history":[{"count":2,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12350\/revisions"}],"predecessor-version":[{"id":12352,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12350\/revisions\/12352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/12354"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=12350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=12350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}