Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:29:08+00:00 2026-05-28T19:29:08+00:00

I’ve basically got an XML file full of product information for use in an

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:29:09+00:00Added an answer on May 28, 2026 at 7:29 pm

    @ben’s answer is correct, but is a little intense for me. SimpleXMLElement objects are nice because you can easily cast them to an array. So, a simpler solution would be to cast it to an array and use max to determine the highest value in the resulting array:

    $str = '
    <item>
        <web_category1>3</web_category1>
        <web_category2>1</web_category2>
        <web_category3>6</web_category3>
    </item>
    ';
    
    $xml = new SimpleXMLElement($str);
    echo max((array)$xml); // outputs: 6
    

    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 use SimpleXMLElement::xpathdocs to get an array of all the occurrences of <item> then execute the same casting trick inside a loop over the xpath result:

    $str = '
    <xml>
      <product1>
        <item>
          <web_category1>3</web_category1>
          <web_category2>1</web_category2>
          <web_category3>6</web_category3>
        </item>
      </product1>
      <product2>
        <item>
          <web_category4>17</web_category4>
          <web_category5>0</web_category5>
        </item>
      </product2>
    </xml>
    ';
    
    $xml = new SimpleXMLElement($str);
    
    $allItems = array();
    $items = $xml->xpath('//item');
    foreach($items as $item) {
      $allItems = array_merge($allItems, (array)$item);
    }
    echo max($allItems); // outputs: 17
    

    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:

    $str = '
    <xml>
      <product1>
        <web_category1>3</web_category1>
        <web_category2>1</web_category2>
        <web_category3>6</web_category3>
      </product1>
      <product2>
        <web_category4>17</web_category4>
        <web_category5>0</web_category5>
      </product2>
      <product3>
        <web_category6>17</web_category6>
        <web_category7>21</web_category7>
      </product3>
    </xml>
    ';
    
    $xml = new SimpleXMLElement($str);
    
    // assumes that product node names start with "product"
    $products = $xml->xpath("//*[starts-with(name(),'product')]");
    foreach ($products as $p) {
      $catNames = array_keys((array)$p);
      $catNums  = preg_replace("/[^\d]/", "", $catNames);
      echo $p->getName() . ' - highest category: ' . max($catNums) . "\n";
    }
    

    The above code outputs the following:

    product1 - highest category: 3
    product2 - highest category: 5
    product3 - highest category: 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.