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

  • Home
  • SEARCH
  • 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 8290705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:53:21+00:00 2026-06-08T12:53:21+00:00

I have a category tree in XML like <cat name=’cat_1′ id=’1′> <cat name=’cat_2′ id=’2′>

  • 0

I have a category tree in XML like

<cat name='cat_1' id='1'>

 <cat name='cat_2' id='2'>
   <cat name='cat_3' id='3'>

   </cat>
 </cat>

 <cat name='cat_4' id='4'>
   <cat name='cat_5' id='5'>

      <cat name='cat_6' id='6'>
        <cat name='cat_7' id='7'>

        </cat>
      </cat>
  </cat>  
 </cat>

</cat>

now I want to implement code such that I can travers this tree an find categories ids by the name i.e. If input is cat_6 than result should be 6;

What I have done

$xmlCatTree // is my simple xml object

class MyClass{   
 public function traverseForId($cat_name , $xmlCatTree )
      {

        if($xmlCatTree->attributes()->name == $cate_name)
            {
              return $xmlCatTree->attributes()->id;
            }
        if(count($item->children())>0){
        foreach($item->children() as $child)
        {
             return $this->traverseForId($cat_name,$child);
        } } 

      } 

}

It works perfectly for single branch i.e child nodes of cat_2 will have correct results but if I search for cat_7 nothing will return. I thing It happens due to branched recursion. I want to know how to cop this.

  • 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-06-08T12:53:22+00:00Added an answer on June 8, 2026 at 12:53 pm

    It seems, that you are using SimpleXML, thus I recommend to directly query the tree.

    foreach ($xmlCatTree->xpath('//cat[@name=' . $cat_name . ']') as $node)
      return $node->attributes()->id;
    }
    return null;
    

    This is even more elegant, because it directly reflects, what you are trying to achieve: Give me all the nodes with attribute “name” with the value “$cat_name“.

    Or (because it seems, that the ID is already part of the category name ;))

    list($unusedThingy, $id) = array_pad(explode('_', $cat_name, 2), 2, null);
    return $id;
    

    But I guess thats not really, what you are looking for 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a category tree and i'd like to get all products that are
I have a class (Person) and a XML tree: <rows> <row> <number>1110</number> <name>Street</name> <value>First
I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
I have a category/file tree structure. Both categories and files can have parents, so
I have category model that has a tree structure. In my database I have
I have a web application with a category tree that is published on the
I have the following table: CREATE TABLE [dbo].[Tree]( [AutoID] [int] IDENTITY(1,1) NOT NULL, [Category]
I have a window with a tree (each node is a category). I have
I have a category model. I am using ancestry gem to make it like
I have nested repeaters and checkbox in each row(like a tree view or ctaegory

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.