I am having a tough time figuring this one out, any help?
I am passing some data into a looping function and want to return the result to the original call.
This only works if a result is found on the first loop, if the function calls itself again I get nothing returned. Though, if I echo the result I see that I have a match.
function fetchAllOffers () {
/* do a bunch of things */
$result = currentOffer($campaign, $campaignsArray); // <-- return result
if ($result) {
/* do more */
}
}
function currentOffer ($step, $campaigns) {
foreach ($campaigns as $c) {
if (/* an exact match is found */) {
return $c['id'];
}
elseif (/* half match move to next step */) {
currentOffer($c, $campaigns);
}
}
return $c['final']; // if no match found return
}
fetchAllOffers();
You have to return result of currentOffer()
in your function.
function currentOffer ($step, $campaigns)
{
foreach ($campaigns as $c)
{
if (/* a match is found */)
return $c['id'];
else
return currentOffer($c, $campaigns);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments