Call current close from close

In javascript you can do something like this

arr.map(function(val) { return typeof val == 'array' ? val.map(arguments.callee) : val.doSomething(); }); 

This will recursively pass through arr and apply doSomething to each value.

Is there an equivalent JavaScript arguments.callee in PHP?

+4
source share
3 answers

Is there an equivalent JavaScript arguments.callee in PHP?

You need to pass this function to yourself:

 $func = function($a = 0) use (&$func) { echo "$a\n"; if ($a == 1) { return; } $func(1); }; $func(); /* output: 0 1 */ 
+5
source

I do not believe that there is the equivalent of arguments.callee. If you really need a recursive lambda in php, there is always a y combinator template: http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator

+2
source

Not as requested, but probably more appropriate:

 array_walk_recursive($arr, function (&$val) { $val = doSomething($val); }); 
0
source

Source: https://habr.com/ru/post/1416443/


All Articles