循环函数中的PHP数组


PHP Array in looping function

我有php函数从某个页面获取项目,该项目有分页

function get_content($link){
    $string = file_get_contents($link);
    $regex = '/https?':'/'/[^'" ]+/i';
    preg_match_all($regex, $string, $matches);
    //this is important
    foreach($matches as $final){
        $newarray[] = $final;
    }
    if(strpos($string,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
        get_content($link);
    }
    return $newarray;
} 

问题:

  1. 在这种情况下,是否可以使用循环函数?

  2. 当我尝试它时,为什么我只得到一页数组?我的意思是,如果有5个页面,每个页面都有50个链接,那么当我尝试打印结果时,我只得到50个链接。

感谢

您从未将递归值设置到正在构建的主数组中。并且您根本没有为了更改从中获取数据的文件而更改$link

你需要做一些类似的事情:

if(strpos($result,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
    $sub_array = get_content($link . '?page=x'); // you need some pagination identifier probably
    $newarray = array_merge($new_array, $sub_array);
}