Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 递归函数不执行代码第二次调用及以后_Php - Fatal编程技术网

Php 递归函数不执行代码第二次调用及以后

Php 递归函数不执行代码第二次调用及以后,php,Php,因此,我有一个文档数组,每个文档添加一个字符串作为数组中的一个元素,还有一个类的实例,该类有一个私有成员函数add document,它本质上是将文档添加到一个新的添加文档数组中。 函数don()有3个参数,它需要添加到已添加文档数组中的字符串,要从函数内部添加文档的类的实例,以及要添加的更多内容的数组 在psuedo代码中,它是这样的: $contentsAdded = []; //blank array $contents = ['document one text', 'document

因此,我有一个文档数组,每个文档添加一个字符串作为数组中的一个元素,还有一个类的实例,该类有一个私有成员函数add document,它本质上是将文档添加到一个新的添加文档数组中。 函数don()有3个参数,它需要添加到已添加文档数组中的字符串,要从函数内部添加文档的类的实例,以及要添加的更多内容的数组

在psuedo代码中,它是这样的:

$contentsAdded = []; //blank array
$contents = ['document one text', 'document two text', 'document three text';
$firstDoc = 'hello I am the first document';

function don($currentString, $instance, $contentArray){
    //addDocument() adds strings to  /         //$contentsAdded
    $instance->addDocument($currentString);
    //Travel through array $contentArray
    //if $contentArray[iterator] is not in     //$contentsAdded, then
    don($contentArray[i], $instance, $contentArray);
}
don($firstDoc, $instance, $contents);
最好的方法是什么


特别是当我按照我的计算方式执行时,$contentsAdded中只有$firstDoc?

您不需要将第一个文档与另一个文档分开,因为您必须创建一个通用流程。在这种情况下,不需要进行递归操作(递归函数可能会占用更多资源,请谨慎使用)

您应该简单地迭代文档数组,并使用实例对象逐个添加它们

// Initialize the list of document to add
$contents = array('first document',
                  'document one text',
                  'document two text',
                  'document three text');

// Loop over your array of document
foreach($contents as $oneDocument)
    $instance->addDocument($oneDocument);

在$contents中您没有错过]吗?您没有返回点。它要么失败,要么永远循环。您的代码有点不一致。什么是
$instance
?其中初始化了
$i
?使用
$contentArray[i]
时,
i
之前的
$
将丢失。。