Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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_Arrays_Recursion - Fatal编程技术网

将结果放入单个数组中的PHP递归

将结果放入单个数组中的PHP递归,php,arrays,recursion,Php,Arrays,Recursion,我正在尝试编写一个网络爬虫程序,但我不知道如何创建一个递归来解析网页并将所有最终结果添加到最终数组中。 我以前从未使用过php,但我在互联网上做了大量的研究,并且已经找到了解析我想要抓取的页面的方法。 请注意,我已经将下面的$url值和数组结果更改为我脑海中随机生成的一些值 此脚本工作正常,并输出以下内容: 数组 ( [0]=>阵列 ( [0]=>文件夹 [1] => /1 [2] => https://www.scrapesite.com/pagetoscrape/sjdfi327943s

我正在尝试编写一个网络爬虫程序,但我不知道如何创建一个递归来解析网页并将所有最终结果添加到最终数组中。 我以前从未使用过php,但我在互联网上做了大量的研究,并且已经找到了解析我想要抓取的页面的方法。
请注意,我已经将下面的$url值和数组结果更改为我脑海中随机生成的一些值


此脚本工作正常,并输出以下内容:

数组
(
[0]=>阵列
(
[0]=>文件夹
[1] => /1
[2] => https://www.scrapesite.com/pagetoscrape/sjdfi327943sad/index.html
)
[1] =>阵列
(
[0]=>文件夹
[1] => /2
[2] => https://www.scrapesite.com/pagetoscrape/345fdsjjsdfsdf/index.html
)
[2] =>阵列
(
[0]=>文件夹
[1] => /3
[2] => https://www.scrapesite.com/pagetoscrape/46589dsjodsiods/index.html
)
[3] =>阵列
(
[0]=>文件夹
[1] => /4
[2] => https://www.scrapesite.com/pagetoscrape/345897dujfosfsd/index.html
)
[4] =>阵列
(
[0]=>文件夹
[1] => /5
[2] => https://www.scrapesite.com/pagetoscrape/9dsfghshdfsds3/index.html
)
)

现在,脚本应该为上述数组中的每个项目执行getFolders函数。这可能会返回另一个文件夹数组,该数组也应该被解析。 然后我想创建一个最后的数组,其中列出了所有文件夹的绝对路径($basepath。“/”$value->filename)和href链接

我真的很感激每一个小小的暗示。
我能够在web上找到一些示例,但我不知道如何在这里实现它,因为我几乎没有一般编程语言的经验。

初始化空数组,并将其作为对
getFolders()
函数的引用传递。继续将刮取结果放入此数组中。另外,您需要在
getFolders()
foreach
循环中再次调用
getFolders()
。示例如下:

$finalResults = array();
getFolders("", parseLink($url), $finalResults);
您的
getFolders()
函数签名现在如下所示:

function getFolders($basepath, $data, &$finalResults) //notice the & before the $finalResults used for passing by reference
并且,您的foreach循环:

foreach ($data as $value) {
    $finalResults[] = array("folder", $basepath . "/" . $value->filename, $value->href);
    getFolders("", parseLink($value->href), $finalResults);
}

上面的代码只是一个示例。根据您的需要更改它。

您能告诉我您希望从此
中得到什么样的预期结果吗?此脚本工作正常,并输出以下数组
?例如,在数组的[0]中有[2],这是一个链接。基本上,我想对[2]中的所有链接执行getFolders函数。但它应该是真正的递归。因此,如果所述函数通过数组中的所有链接执行,那么它也应该一次又一次地(递归)为结果链接执行该函数。在这个过程中,应该会产生一个包含所有链接和腐蚀路径的最终数组。非常感谢,这正是我想要的(递归)。我稍微修改了代码以更新$basepath,现在一切都正常了。你是我的英雄:)