Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say I have 2 functions that can call each other

PHP
public static function goToAction($action,$sender_id)
    {
        $actions = array();
        $logic = file_get_contents('../../logic/logic.json');
        $logic_array = json_decode($logic, true);
        unset($logic);
        if (!isset($logic_array[$action])) {
            return false;
        } else {
            foreach ($logic_array[$action] as $action) {
                $actions[] = self::parseActionType($action,$sender_id);
            }
        }
        return $actions;
    }
    

     public static function parseActionType($actions,$sender_id)
    {
        $data = array();
        foreach ($actions as $key => $action) {
            switch ($key) {
                case 'goto': { 
                          $goto_actions = self::goToAction($action,$sender_id);
                          foreach ($goto_actions as $goto_action){
                          $data[] = $goto_action;
                      } break;
                      ...
       }
    }
    return $data;
    }


and here is my json file:

JavaScript
"no_return": [
    { "text": "Должно быть: 1, 2, 3"},
    { "text": "1" },
    { "goto": "2nr", "no_return": true},
    { "text": "5" }
    ],
    "2nr": [
    { "text": "2" },
    { "goto": "3", "no_return": true},
    { "text": "4"}
    ],
    "3nr": [
    { "text": "3" }
    ],

it returns 12345 , and its right, but how can I make it return 123 if `no_return` is setted to `true`?

What I have tried:

I dont even know how do i resolve it
Posted
Updated 22-Dec-17 2:34am

1 solution

You handle it like any other loop - you have a test as to whether it continues to loop or terminates.

For any recursive function (on or two, same idea) it's always the same. Build in a fail-safe or watch it crash and burn.

So - what can you test for that will allow you to control the looping?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900