The return true; is not ending the current function loop! This is so basic but I don't find why break is not working!
I try this piece of code:
function findKey($array, $keySearch) {
foreach ($array as $key => $item){
echo "$key == $keySearch";
if ("$key" == "$keySearch"){
echo 'yes, it exists';
return true;
}
else
{
if (is_array($item))
findKey($item, $keySearch);
}
}
return false;
}
The return true;
never break as it let it go after the yes, it exists
is echoed!
Solved
The code itself from an accepted solution here was wrong. It has been corrected. Also, below you will find the right answer.