php 递归函数:调用php函数本身

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18634775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:56:26  来源:igfitidea点击:

Recursive function: Call php function itself

phpfunctioncallself-reference

提问by Andrew Surdu

I just want to make sure I do it right and this will not create any conficts.

我只是想确保我做对了,这不会造成任何冲突。

I have a function which calls itself and need your approval if it's OK or not to do so?

我有一个调用自己的函数,如果可以这样做,需要您的批准吗?

<?php

function determine($the_array){
    foreach ($the_array as $key => $value) {
        switch ($key) {
            case 'in':
                    echo $value;
                break;

            case 'out':
                    echo $value;
                break;

            case 'level':
                    echo '<ul>';
                    determine($value);
                    echo '</ul>';
                break;

        }
    }

}

This is the array:

这是数组:

$the_array = array(
    'in' => '<li>Simple IN</li>',
    'out' => '<li>Simple OUT</li>',
    'level' => array(
            'in' => '<li>Simple IN 2</li>',
            'out' => '<li>Simple OUT 2</li>',
            'level' => array(
                'in' => '<li>Simple IN 3</li>',
                'out' => '<li>Simple OUT 3</li>'
            ),
        ),
);

And here is the final init:

这是最终的初始化:

echo '<ul>';
determine($the_array);
echo '</ul>';

The result is just how I wanted to be, it works great, but I don't know if this is a good practice.

结果正是我想要的样子,效果很好,但我不知道这是否是一个好的做法。

回答by Fluffeh

Recursive functions are OK - but dangerous if you aren't sure you know what you are doing. If there is any chance that a function will end up in a recursive loop (where it keeps calling itself over and over) you will either time out, run out of memory or cause a zombie apocalypse.

递归函数是可以的 - 但如果你不确定自己在做什么,则很危险。如果一个函数有可能以递归循环结束(它不断地反复调用自己),你要么超时,要么内存不足,要么导致僵尸末日。

Think of recursive calls as a really, really sharp knife - in the hands of an experienced chef, it's a match made in heaven, in the hands of the dishwasher, it is a lost finger waiting to happen.

把递归调用想象成一把非常非常锋利的刀——在经验丰富的厨师手中,这是天作之合,在洗碗机的手中,它是等待发生的失去的手指。

PHP tries to play nice, and limits a recursive depth to 100 by default (though this can be changed) but for almost all cases, if your recursive depth gets to 100, the accident has already happened and PHP reacts by stopping any additional pedestrians from wandering into traffic. :)

PHP 试图表现得很好,默认情况下将递归深度限制为 100(尽管可以更改)但对于几乎所有情况,如果您的递归深度达到 100,则事故已经发生,PHP 的反应是阻止任何额外的行人从在车流中徘徊。:)

回答by Kleskowy

Fluffeh provided sufficient answer as far as recursive functions are concerned. But when using recursion with large arrays/objects/etc, you should watch optimisation of your code, so that it doesn't take much memory or CPU power to execute.

就递归函数而言,Fluffeh 提供了足够的答案。但是当对大型数组/对象/等使用递归时,您应该注意代码的优化,以便执行时不会占用太多内存或 CPU 能力。

You could easily optimise your code to be cleaner, take less memory and be more resilient to unexpected data. Notice the & in the function arguments list (it eliminates creating a copy of an array everytime a nested function is called).

您可以轻松优化代码,使其更简洁、占用更少内存并对意外数据更具弹性。注意函数参数列表中的 &(它消除了每次调用嵌套函数时都创建数组的副本)。

function determine(& $the_array){
foreach ($the_array as $key => $value) {
    switch ($key) {
        case 'in':
        case 'out':
                echo $value;
            break;

        case 'level':
            if (!is_array($value)) break;
                echo '<ul>';
                determine($value);
                echo '</ul>';
            break;

        }
    }
}

回答by T.Todua

I dont know, if it's a good solution, but i use this one to call a function from inside itself:

我不知道,这是否是一个好的解决方案,但我使用这个从内部调用一个函数:

function my_calucar(){
    $arrayy= array('mine' => '1',  'yours' => '24', 'her' => '34');
    foreach ($arrayy as $each=>$value) {
        switch ($each) {
        default:
                my_calucar($value);
        }
    }
}

回答by MaxEcho

I think if you know depth of array it is good to use

我认为如果您知道数组的深度,则可以很好地使用

$list = "";
foreach ($the_array as $array) {
    if(is_array($array)) {
       foreach($array as $sub_array) {
          if(is_array($sub_array)) {
              foreach($sub_array as $sub_array_2) {
                  $list .= "$sub_array_2";
               }
          } else {
             $list .= "$sub_array";
          }
       }
    } else {
        $list .= "$array";
    }
}

echo "<ul>$list</ul>";