PHP - 将每个变量变量传递给new foreach

时间:2016-01-30 20:27:23

标签: php simple-html-dom

如何将每个变量传递给新的foreach

我有这个:

<?php
include_once('js/simple_html_dom.php');

$html = file_get_html('http://www.homepage.com/');

foreach($html->find('figure a') as $eventLink)                  
    echo $urlVariable.$eventLink->href . "<br>";                        
?>

这样可行,结果:

http://www.homepage.com/link1
http://www.homepage.com/link2
http://www.homepage.com/link3
http://www.homepage.com/link4
...

期望:所有链接作为新foreach函数的变量

<?php
include_once('js/simple_html_dom.php');

$html = file_get_html('http://www.homepage.com/');

foreach($html->find('figure a') as $eventLink)                  
    echo $urlVariable.$eventLink->href . "<br>";                        



// Desired:

$newGrabbedLinks = .... // (should be ALL grabbed links equal to: echo $urlVariable.$eventLink->href )

foreach($newGrabbedLinks->find('.text') as $newTexts)   
        echo $newTexts->innertext;

?>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

嗯,我认为你所说的是阵列。

$links = array();
$html = file_get_html('http://www.homepage.com/');

foreach($html->find('figure a') as $eventLink)                  
    $links[] = $urlVariable.$eventLink->href;   

不是回应你,而是将每个值添加到数组中的某个位置。

要将数组添加到foreach函数,您只需执行

foreach($item in $links)
echo $item