Click here to Skip to main content
14,027,612 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Let's say I have 2 functions that can call each other

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:

"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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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?
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190419.4 | Last Updated 22 Dec 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100