当谈到php时,我很绿,所以这里有一个基本的问题,我还没有找到答案。
我通过进行正则表达式匹配来从页面中提取重复信息,如下所示。正则表达式有两个捕获组(...),因此$matches数组索引范围从0(完整正则表达式值)到2(1和2是捕获组值)。
$page = file_get_contents(url);
preg_match_all(regex-with-two-capturing-groups, $page, $matches);
$m1 = $matches[1];
$m2 = $matches[2];
for ($i=0; $i < sizeof($m1); $i++) {
echo ("
$m1[$i]$m2[$i]");}
?>这工作正常,但起初我尝试了一个较短的版本:
$page = file_get_contents(url);
preg_match_all(regex-with-two-capturing-groups, $page, $matches);
for ($i=0; $i < sizeof($matches[0]); $i++) {
echo ("
$matches[1][$i]$matches[2][$i]");}
?>但是这导致了
Array[0]Array[0]Array[1]Array[1]Array[2]Array[2]Array[3]Array[3]Array[4]Array[4]...这是为什么?
有没有办法在一个表达式中进行二维索引,而不必定义中间数组m1和m2?