2013-04-15 2 views
0

그래서 각 문자열을 배열의 요소로 추가하고 private 멤버 함수가있는 클래스의 인스턴스를 추가하여 문서를 추가합니다. 추가 된 문서의 새로운 배열로. 함수 don()은 3 개의 인수, 추가 된 문서의 배열에 추가해야하는 문자열, 함수 내부에서 문서를 추가 할 클래스의 인스턴스 및 추가 할 내용의 배열을 취합니다.재귀 함수가 두 번째 호출 이상 코드를 실행하지 않음

$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 만 있습니다.

+1

$ 콘텐츠를 놓치지 않으셨습니까? –

+0

반송 지점이 없습니다. 실패하거나 반복됩니다. –

+0

코드가 약간 일관성이 없습니다. '$ instance'는 무엇입니까? '$ i'는 어디에 초기화됩니까? '$ contentArray [i]'를 사용하면'i' 앞에'$'가 없습니다 .. – MatRt

답변

1

일반 프로세스를 만들어야하므로 첫 번째 문서를 다른 문서와 구분할 필요가 없습니다. 그리고이 경우에는 재귀 적 fonction을 만들 필요가 없습니다 (재귀 함수는 잠재적으로 더 많은 리소스를 사용하고주의해서 사용하십시오).

문서 배열을 반복하여 인스턴스 오브젝트를 사용하여 하나씩 추가해야합니다.

// 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); 
관련 문제