php looping a function and return result to original call

Justin Herter

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();
fdehanne

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call another function or return 2 result

From Dev

Looping function if the function result is empty

From Dev

PHP function shows result but does not return it

From Dev

PHP Function that call itself does not return anything

From Dev

Php Looping debugging not desire result

From Dev

In PHP are recalls to any given function the variable they return or a call to the function itself

From Dev

Not able to call php function after database query result codeigniter

From Dev

OOP in PHP - variables with methods result in fatal call to member function

From Dev

Polymer function call but no result

From Dev

Return result to parent function

From Dev

Return result of Action to Function

From Dev

Return mongoose result in a function

From Dev

wrapAsync return function not result

From Dev

looping php mysql query till no result found

From Dev

PHP: Loop looping through result set

From Dev

looping array value with function php

From Dev

Call Modal windows and return to original windows

From Dev

Strange looping behavior with Preserve function call in Vim

From Dev

Is it good to return a function call?

From Dev

how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

From Dev

How to return value from function to call it sequentially each line with new result

From Dev

Call a php and return immediately

From Dev

How to structure a looping API call to make the JSON result usable?

From Dev

For looping through array doesn't return correct result

From Dev

View Won't Return a Result After Looping Through a Collection

From Dev

Call to undefined function result() in Codeigniter

From Dev

How can I use PHP call python script return result in realtime?

From Dev

How can I use PHP call python script return result in realtime?

From Dev

Return stored procedure result in function

Related Related

  1. 1

    Call another function or return 2 result

  2. 2

    Looping function if the function result is empty

  3. 3

    PHP function shows result but does not return it

  4. 4

    PHP Function that call itself does not return anything

  5. 5

    Php Looping debugging not desire result

  6. 6

    In PHP are recalls to any given function the variable they return or a call to the function itself

  7. 7

    Not able to call php function after database query result codeigniter

  8. 8

    OOP in PHP - variables with methods result in fatal call to member function

  9. 9

    Polymer function call but no result

  10. 10

    Return result to parent function

  11. 11

    Return result of Action to Function

  12. 12

    Return mongoose result in a function

  13. 13

    wrapAsync return function not result

  14. 14

    looping php mysql query till no result found

  15. 15

    PHP: Loop looping through result set

  16. 16

    looping array value with function php

  17. 17

    Call Modal windows and return to original windows

  18. 18

    Strange looping behavior with Preserve function call in Vim

  19. 19

    Is it good to return a function call?

  20. 20

    how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

  21. 21

    How to return value from function to call it sequentially each line with new result

  22. 22

    Call a php and return immediately

  23. 23

    How to structure a looping API call to make the JSON result usable?

  24. 24

    For looping through array doesn't return correct result

  25. 25

    View Won't Return a Result After Looping Through a Collection

  26. 26

    Call to undefined function result() in Codeigniter

  27. 27

    How can I use PHP call python script return result in realtime?

  28. 28

    How can I use PHP call python script return result in realtime?

  29. 29

    Return stored procedure result in function

HotTag

Archive