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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:38:32+00:00 2026-05-13T22:38:32+00:00

I have a recursion function that parses an object/array with a global variable. If

  • 0

I have a recursion function that parses an object/array with a global variable. If I comment out the global variable I get nothing but if I leave it in it keeps adding to the array other values that should be in it own result set. Do I need to change something here?

UPDATE #2:
How can I get the return I want, I thought I was pushing all unique values to the array?

function getResp($objectPassed) {

    foreach($objectPassed as $element) {
        if(is_object($element)) {
            // recursive call
            $in_arr = getResp($element);
        }elseif(is_array($element)) {
            $in_arr = getResp($element);
        } else {
            // XML is being passed, need to strip it
            $element = strip_tags($element); 

            // Trim whitespace
            $element = trim($element);

            // Push to array
            if($element != '') {
                if (!preg_match("/^[0-9]$/", $element)) {
                    if (!in_array($element,$in_arr)) {
                        $in_arr[] = $element;
                    }
                }
            } 
        }
    }
    return $in_arr;
}

INPUT:

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => typeName
                    [Id] => Array
                        (
                            [0] => a0E50000002jxhmEAA
                            [1] => a0E50000002jxhmEAA
                        )

                )

            [1] => stdClass Object
                (
                    [type] => typeName
                    [Id] => Array
                        (
                            [0] => a0E50000002jxYkEAI
                            [1] => a0E50000002jxYkEAI
                        )

                )

        )

    [size] => 2
)

RETURN:

Array
(
    [0] => a0E50000002jxYkEAI
)

WANTED RETURN:

Array
(
    [0] => a0E50000002jxYkEAI
    [1] => a0E50000002jxhmEAA
)
  • 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-13T22:38:32+00:00Added an answer on May 13, 2026 at 10:38 pm

    Is a global variable necessary? Otherwise you could simplify it this way:

    function getResp($objectPassed, &$in_arr = array()) {  // <-- note the reference '&'
    
        foreach($objectPassed as $element) {
            if(is_object($element) || is_array($element)) { // <-- else if statement simplified
                getResp($element,$in_arr);
            } else {
                // XML is being passed, need to strip it
                $element = strip_tags($element); 
    
                // Trim whitespace
                $element = trim($element);
    
                // Push to array  
                if($element != ''  &&                      // <-- everything in one test
                  !preg_match("/^[0-9]$/", $element) &&
                  !in_array($element,$in_arr)) 
                {                    
                      $in_arr[] = $element;  
                } 
            }
        }
        return $in_arr;
    }
    

    Then you do:

    $result = getResp($data);
    

    If a recursive function has to access the same resource over and over again (in this case the initial array), I would always pass this as a reference.
    I don’t know if is measurable but I would guess that this is much more efficient than copying values.

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

Sidebar

Related Questions

I have a recursive function that parses a trie string database replacing with all
Lets say I have a recursive function that creates lists within lists. It return
I have a simple recursive function RCompare() that calls a more complex function Compare()
Is it possible to have a PHP function that is both recursive and anonymous?
After some recursive function I have an array: first iteration: Array ( [category_id] =>
I have some sort of recursive function, but I need to parse a string,
So I have built a recursive function that generates a collection of Category objects.
I want a function that searches through my array, and returns all the children
I have an array that lists folders in a directory. Until now, I've been
I have a generic class called Repository. This class has a function that calls

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.