Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
如何使用while循环PHP将数据推送到数组_Php_Arrays_While Loop - Fatal编程技术网

如何使用while循环PHP将数据推送到数组

如何使用while循环PHP将数据推送到数组,php,arrays,while-loop,Php,Arrays,While Loop,我想用preg\u match\u all()抓取几个页面,并将数据保存到一个数组($matches),url如下所示: webpage.com/p/1,最后的数字是子页面的数量,每次运行时必须更改。 我考虑过类似的事情(有10个子页面): $x=1; 而($x) $x以前是一个静态字符串,在循环中没有更改。您可以尝试类似于以下内容的操作-确保不会覆盖正在发生的$matches $data=array(); $regex=''; for( $i=0; $i < 10; $i++ ){

我想用
preg\u match\u all()
抓取几个页面,并将数据保存到一个数组(
$matches
),url如下所示:
webpage.com/p/1
,最后的数字是子页面的数量,每次运行时必须更改。 我考虑过类似的事情(有10个子页面):

$x=1;
而($x)

$x以前是一个静态字符串,在循环中没有更改。

您可以尝试类似于以下内容的操作-确保不会覆盖正在发生的
$matches

$data=array();
$regex='';
for( $i=0; $i < 10; $i++ ){
    $html = file_get_contents( 'http://www.webpage.com/p/'.$i );    
    preg_match_all( $regex, $html, $matches );
    $data[] = count( $matches ) > 1 ? $matches[1] : false;
)
$data=array_filter($data);
print_r( $data );
$data=array();
$regex='';
对于($i=0;$i<10;$i++){
$html=文件\u获取\u内容('http://www.webpage.com/p/"(一元),;
preg_match_all($regex,$html,$matches);
$data[]=计数($matches)>1?$matches[1]:false;
)
$data=阵列过滤器($data);
打印(数据);

$matches=$matches[1]
你知道这段代码是做什么的吗?检查array_push()$matches是一个字符串。你需要使用
$allmatches[]=$matches[1];
我指的是最后一行中的匹配项。在两件事上使用相同的名称确实令人困惑。正如你所看到的,这是一件糟糕的事情当我喜欢@Andreas建议我得到6个具有相同元素的数组时,从第1页匹配的数组你在HTML上使用正则表达式吗?你也可以用"它的代码会很好。@这不是你所缺的所有东西。你在HTML上使用正则表达式,它会回来咬你的。正确地完成它并不是很多工作。你用ReXEX的匹配来覆盖你的字符串。不幸的是,这是项目的要求,我必须用正则表达式来完成。顺便说一下,我看到了关于PAR的帖子。用正则表达式唱html,知道它不好,只需满足..@sh00t要求?由谁?您的客户?如果您不喜欢dom,可以使用substr()、strpos()和strip_tags()。我很确定这至少会起作用。老师不是客户,我想如果我想拥有客户,我的技能水平需要很大的提高;)不管怎样,我成功了,谢谢你提供有关substr和STRPO的提示,我会研究:)
$current_page = 'webpage.com/p/'.$x;
$data=array();
$regex='';
for( $i=0; $i < 10; $i++ ){
    $html = file_get_contents( 'http://www.webpage.com/p/'.$i );    
    preg_match_all( $regex, $html, $matches );
    $data[] = count( $matches ) > 1 ? $matches[1] : false;
)
$data=array_filter($data);
print_r( $data );