I have a function
function GetArrKey( $findArr, $key_arr, $depth=0 )
{
if( count($key_arr) <= $depth || !array_key_exists($key_arr[$depth], $findArr) )
return NULL;
else if( count($key_arr) == $depth+1 )
return $findArr[$key_arr[$depth]];
return self::GetArrKey( $findArr[$key_arr[$depth]], $key_arr, $depth+1 );
}
That searches an array and returns the part of the array that matches what I need.
The only problem is it seems to be returning the exact same value. For example, I have this function in a foreach loop
foreach($e as $k => $v) {
$value = GetArrayKey(array,$k);
print_r($value);
}
and it’s printing the exact same value 5 times (but the $k that I am using to search for is different each time).
I’m assuming it is because of the return self::GetArrKey but I can’t seem to fix it.
Examples…
tweet-19486731414564564
Tweet 1
tweet-19486778435455556
Tweet 1
tweet-19703966855465458
Tweet 1
tweet-19842914654654650
Tweet 1
But it should do
tweet-19486731414564564
Tweet 1
tweet-19486778435455556
Tweet 2
tweet-19703966855465458
Tweet 3
tweet-19842914654654650
Tweet 4
I believe your problem is in the last line of your recursive function:
This means that the function will be called again, until you hit a base case. The base case will return, and this function does not modify that case. Therefore all calls to the recursive function will return only the result of the base case, which is not what you want in this case because you’re looking for a response rather than performing some function recursively.