HTMLページを再帰的に開いて記事を抽出する機能があります。関数を実行した後、返された配列は NULL ですが、その間のトレース ステップは、配列に実際に要素が含まれていることを示しています。配列を返すとリセットされると思います。
関数内の要素を含む配列が、返された後に NULL になるのはなぜですか?
これは関数です(簡略化):
function get_content($id,$page=1){
global $content; // store content in a global variable so we can use this function recursively
// If $page > 1 : we are in recursion
// If $page = 1 : we are just starting
if ($page==1) {
$content = array();
}
$html = $this->open($id,$page)) {
$content = array_merge($content, $this->extract_content($html));
$count = count($content);
echo("We now have {$count} articles total.");
if($this->has_more($html)) {
$this->get_content($id,$page+1);
} else {
$count = count($content);
echo("Finished. Found {$count} articles total. Returning results.");
return $content;
}
}
これは私が関数を呼び出す方法です:
$x = new Extractor();
$articles = $x->get_content(1991);
var_export($articles);
この関数呼び出しは、次のような出力を出力します。
We now have 15 articles total.
We now have 30 articles total.
We now have 41 articles total.
Finished. Found 41 articles total. Returning results.
NULL
関数内の要素を含む配列が、返された後に NULL になるのはなぜですか?