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 7066463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:05:07+00:00 2026-05-28T05:05:07+00:00

I have an object containing nested objects and so on. The level of nesting

  • 0

I have an object containing nested objects and so on. The level of nesting can be assumed to be infinite.

If the object nests other objects, then the children should be stored in an array property called childElements.

The object I am working with looks like this:

Object
   childElements
       ['object1'] => object
                         childElements
                            ['object11'] => object

       ['object2'] => object

I would like to get a reference to the object called object11 using recursion. This is the function I am using. The function resides in a class, so $this is used when calling the recursion:

public function recursiveSearch(array $childElements, $elementName){
    foreach ($childElements as $key => $element) {
        var_dump($key);
        if (isset($element->childElements)){

            return $this->recursiveSearch($element->childElements, $elementName);

        }else{
            if ($key == $elementName){
                return $childElements[$elementName];
            }
        }
    }

    throw new Exception("$elementName could not be found.");
}

I then call my function like so (assuming that object is called $r):

return $this->recursiveSearch($r->childElements, 'object11');

The problem with my code (when looking at the var dumps is that the function will keep travelling towards the inner most object. But once that is done, it terminates, regardless of whether it has visited any other childElements. I believe the issue is due to return $this->recursiveSearch which forces a return prematurely.

How can I structure my recursive function so that it works properly?

  • 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-28T05:05:08+00:00Added an answer on May 28, 2026 at 5:05 am

    The problem is that your code assumes that if an element has child elements, the element must be in the child elements. You actually need three conditions though:

    • if the element itself is the searched-for element, return it
    • if the element has children and the searched-for element is in those children, return it
    • if none of the above for all elements, return false (don’t throw an exception, because not finding the element is to be expected on most children)

    Code:

    public function recursiveSearch(array $childElements, $elementName){
        foreach ($childElements as $key => $element) {
            if ($key == $elementName) {
                return $element;
            }
            if (
                !empty($element->childElements) &&
                $element = $this->recursiveSearch($element->childElements, $elementName)
            ) {
                return $element;
            }
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewModel that have a list object containing other list objects and
I have Cusom collection object containing a collection of objects. I then have two
Suppose I have an object containing some data. How can I see that data
I have HTML in a JavaScript string (containing usual, nested HTML). Using jQuery, can
I have a work-space object containing a list of target objects with their properties.
I have a ResultSet object containing all the rows returned from an sql query.
I have a populated SqlCommand object containing the command text and parameters of various
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a object built through a factory containing my parameters read from the
I have a GridView where one column is bound to an object property containing

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.