I want to fill an array with the links that I get from this foreach. How can I do that?
foreach($html->find('a') as $link) {
echo $link->href; //output: link1.html link2.html link3.html......
}
所有你需要的是
$links = array_map(function($v){return $v->href;}, $html->find('a'));
I'd say array_map() is the best way:
$links = array_map(function($link) { return $link->href; }, $html->find('a'));
It takes everything in the given array (in this case, $html->find('a')
) and returns a new array based on a map using the function you give it (in this case, function($link) { return $link->href; }
). It applies that function to each element in the given array to create each element in the returned array.
With array_push
. See this link
<?php
$stack = array();
foreach($html->find('a') as $link) {
array_push($stack, $link->href);
}
print_r($stack);
?>
It's simple, Try this :-
$dataArray = array();
foreach($html->find('a') as $link) {
$dataArray[] = $link->href;
}
echo '<pre>';
print_r($dataArray);
echo '</pre>';
把它放在循环中:$ links [] = $ link-> href;
$hrefs=array();
foreach($html->find('a') as $link) {
$hrefs[]= $link->href; //output: link1.html link2.html link3.html......
}
That should do it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.