Php 正则表达式(preg_match_all)和数组

Php 正则表达式(preg_match_all)和数组,php,arrays,multidimensional-array,preg-match,preg-match-all,Php,Arrays,Multidimensional Array,Preg Match,Preg Match All,代码如下: <?php $matchWith = "# http://videosite.com/id1 http://videosite.com/id2 # # http://videosite.com/id3 http://videosite.com/id4 #"; preg_match_all('%(#)\shttp://videosite\.com/(\w+)\r{0,1}\shttp://videosite\.com/(\w+)\r{0,1}\s(#)%'

代码如下:

   <?php
    $matchWith = "# http://videosite.com/id1 http://videosite.com/id2 # # http://videosite.com/id3 http://videosite.com/id4 #";

    preg_match_all('%(#)\shttp://videosite\.com/(\w+)\r{0,1}\shttp://videosite\.com/(\w+)\r{0,1}\s(#)%', $matchWith, $result);
    foreach($result[2] as $value)
    {  
    print '<a href="http://videosite.com/'.$value.'">
    First Part
    </a>';        

    }  
    foreach($result[3] as $value)
    {  
    print '<a href="http://videosite.com/'.$value.'">
    Second Part
    </a>';        
    }
    ?>

问题是:

我希望它像这样显示:第一部分,第二部分和第一部分第二部分

但它是这样显示的:第一部分,第一部分,第二部分,第二部分

我真的希望你能理解。 谢谢


答案假设两个阵列具有并行结构

编辑:将$i==更改为$i++

这是您想要的吗

for($i = 0; $i < count($result); $i++)
{  
    print '<a href="http://videosite.com/'.$result[$1][2].'">
    First Part
    </a>';        

    print '<a href="http://videosite.com/'.$result[$1][3].'">
    Second Part
    </a>'; 
}
for($i=0;$i
for循环应该说
$i++
而不是
$i==
,但除此之外,这是正确的。太棒了!这正是我想要的答案!你们太棒了!:)我不确定,因为我收到了一条错误消息。但是有人刚刚发布了我想要的答案。还是谢谢你:)
for($i = 0; $i < count($result); $i++)
{  
    print '<a href="http://videosite.com/'.$result[$1][2].'">
    First Part
    </a>';        

    print '<a href="http://videosite.com/'.$result[$1][3].'">
    Second Part
    </a>'; 
}