Jump to content

Recursion Question


kellan4459

Recommended Posts

I am trying to work through an array of people links and return a certain person based on some comparisons.

 

This is a multidimensional array with some levels having more links than others

 

so you could have

 

it would look more like a tree structure.

 

I have a function

 


public function handlelinkL1Links($linkLinksArray)
{

	foreach($linkLinksArray as $key=>$value)
	{




			if(array_search($key,$this->linkProcessed)===false && $this->compareKey($key)==true)
			{
				$linkToAdd = $key;
			}	

	$this->handlelinkL1Links($value);
	}
	return $linkToAdd;

}

 

 

No matter how I have defined linkToAdd it is always null.  How can I do this without it being global?

Link to comment
Share on other sites

Try something more like

foreach ($array as $key => $value) {
    if ($value is an array) {
        $return = recursive search on $value;
        if ($return means that the search found the value) return $return;
    } else if ($value matches the search value) {
        return whatever you want;
    }
}

Link to comment
Share on other sites

I realized I had a flaw when I looked at your response.  I want know the return value without touching all items.

 

If I have the following

1->4->16

      4->32

200->40

450->209

      ->274->316

                ->490

 

I need to check if the current id is linked to one that has already been added and if so is it the first entered for the current session by date

I am thinking I will need to pass a variable to the function to old the value as I recurse through

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.