I’ve basically got an XML file full of product information for use in an ecommerce system. I’ve been creating a script that converts these XML files into a .CSV with the data structured in a format the ecommerce system can handle (So I don’t need to copy/paste columns over every time the vendor provides new XML files). The category of each product is defined like this:
<web_category1>3</web_category1>
<web_category2>1</web_category2>
<web_category3>6</web_category3>
web_category3 being the category of the item and 1 and 2 being the parent categories of the product’s category. The thing is that some items are nested under 2 categories..or sometimes 5. So I need to figure out a way for PHP to grab the web_category with the highest number after it since that’s always going to be the product’s category.
Thanks!
@ben’s answer is correct, but is a little intense for me.
SimpleXMLElementobjects are nice because you can easily cast them to an array. So, a simpler solution would be to cast it to an array and usemaxto determine the highest value in the resulting array:UPDATE
Based on your comment below, let’s assume you need to get the max of all the
<item>elements that occur in an XML file and not just one (like above). To handle this you could useSimpleXMLElement::xpathdocs to get an array of all the occurrences of<item>then execute the same casting trick inside a loop over the xpath result:UPDATE 2
Okay, last time. If this isn’t exactly what you’re trying to do, you should at least have enough examples to figure it out from here. Consider:
The above code outputs the following: