kellan4459 Posted January 22, 2012 Share Posted January 22, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255501-recursion-question/ Share on other sites More sharing options...
requinix Posted January 22, 2012 Share Posted January 22, 2012 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/255501-recursion-question/#findComment-1309982 Share on other sites More sharing options...
kellan4459 Posted January 22, 2012 Author Share Posted January 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255501-recursion-question/#findComment-1310017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.