O
O
Outsider V.2015-03-18 17:51:59
PHP
Outsider V., 2015-03-18 17:51:59

How to completely exit a function with recursion and an infinite nesting loop?

There is a function that, using recursion, iterates over the elements of an array of unlimited nesting to match a certain criterion. Having found the first element that matches the criterion, the function should immediately return 1.
The problem is that due to recursion, if I write return 1, the value is returned to the "parent" copy of the function, and the loop continues. If I write break 1/2/3/4, etc., I complete only a specific cycle, by nesting relative to the current one, and I can have at least a million of them. Is there any way to command to stop all loops and return the value 1? Is there any universal solution that stops the very first loop (break) or the command to stop the very first copy of the function (return)? PHP 5.6 procedural style.
So far, I have only found a check of the value returned by the called copy - if 1, then all nested copies return 1 to the parent until it reaches the very first one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2015-03-18
@FanatPHP

as plunged into recursion - and get out.
on a chain launched - on a chain and leave.
return search($whatever);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question