0

I have a question about a recursive PHP function. I have an array of ID’s and a function, returning an array of „child id’s“ for the given id.

public function getChildId($id) {
    …
    //do some stuff in db
    …
    return childids;
}

One childid can have childids, too! Now, I want to have an recursive function, collecting all the childids.

I have an array with ids like this:

$myIds = array("1111“,"2222“,"3333“,“4444“,…);

and a funktion:

function getAll($myIds) {

}

What I want: I want an array, containing all the id’s (including an unknown level of childids) on the same level of my array. As long as the getChildId($id)-function is returning ID’s…

I started with my function like this:

function getAll($myIds) {
    $allIds = $myIds;
    foreach($myIds as $mId) {
        $childids = getChildId($mId);

        foreach($childids as $sId) {
            array_push($allIds, $sId);

            //here is my problem.
            //what do I have to do, to make this function rekursive to
            //search for all the childids? 

        }       
    }
    return $allIds;

}

I tried a lot of things, but nothing worked. Can you help me?

3
  • 1
    in your current example, $myIds is a simple flat array, maybe you could add a proper sample Commented Jun 6, 2014 at 14:42
  • 1
    Can you put up a better example of your array on which this function has to run? The current one is just a simple one dimensional array with no depth. Commented Jun 6, 2014 at 14:42
  • $myIds is my array with the given "root"-Ids. From these Ids, I want to search all the child Id's (my function getChildId()) looks in a table in my database for a dataset having the given Id as primary key, so it is a relation to the root id. In my database one "id" can have many child id's and one child id, can have many child ids, too (and so on)) Commented Jun 6, 2014 at 14:52

2 Answers 2

1

Assuming a flat array as in your example, you simply need to call a function that checks each array element to determine if its an array. If it is, the function calls it itself, if not the array element is appended to a result array. Here's an example:

$foo = array(1,2,3,
  array(4,5,
    array(6,7,
      array(8,9,10)
    )
  ),
  11,12
);

$bar = array();
recurse($foo,$bar);

function recurse($a,&$bar){
  foreach($a as $e){
    if(is_array($e)){
      recurse($e,$bar);
    }else{
      $bar[] = $e;
    }
  }
}

var_dump($bar);

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

You need to read the example more carefully. He is not storing all the sub IDs in nested arrays. Instead he has a function to lookup from a DB
0

I think this code should do the trick

function getAll($myIds) {
    $allIds = Array();
    foreach($myIds as $mId) {
        array_push($allIds, $mId);
        $subids = getSubId($mId);

        foreach($subids as $sId) {
            $nestedIds = getAll($sId);
            $allIds = array_merge($allIds, $nestedIds);
        }       
    }
    return $allIds;
}

5 Comments

You should add checks to see if it's still an array, otherwise it's going to produce errors.
where is getSubId() defined?
Thank you very much! This solution helped me a lot! @halabuda: I changed getSubId to getChildId for a better understanding of my problem.
@halabuda sorry, i renamed getChildIds to getSubIds. Hadn't had my morning caffeine yet
@BenFortune Yeah, this is a bit sketchy in that regard, but I was trying to keep close to the question to make it easier to understand. Ideally I would rearrange this so that getSubId is the recursive function, and require that it always be fed 1 ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.