[英]assign element to array in for-each loop if condition satisfied
這是我的代碼
$url = 'link.html'
$html = file_get_html($url);
$dayArray = array('28th Apr 2014', '29th Apr 2014' , '30th Apr 2014' ,'1st May 2014');
foreach($dayArray as $value){
$dayArrayvalue=strtotime($value);
$month_name=date("F",$dayArrayvalue);
foreach($html->find('table',1)->find('a') as $elm) {
if($elm->plaintext == $month_name){
$month_links1= $elm->href;
//array_push($month_links, $month_links1);
$month_links[] = $month_links1;
}
else{
$month_links[] = 0;
}
}
}
print_r( $month_links);
如果月份名稱與月份的日期匹配,我想將每個月份的鏈接存儲在aaray中
在link.html中,第二個表包含帶有鏈接的月份
誰能幫我 ?
試試這個更新的版本,我猜您想要什么,因為您沒有清楚解釋代碼背后的目的是什么。
$url = 'link.html'
$html = file_get_html($url);
//$dayArray = array('28th Apr 2014', '29th Apr 2014' , '30th Apr 2014' ,'1st May 2014');
$monthNameArray = array('April', 'May');
$month_links = array();
foreach($monthNameArray as $month_name){
//$dayArrayvalue=strtotime($value);
//$month_name=date("F",$dayArrayvalue);
foreach($html->find('table',1)->find('a') as $elm) {
if(strcasecmp($elm->plaintext,$month_name)==0){
array_push($month_links, $elm->href);
}
//else{
//this will add a 0 to the array if there is no match. I don't think this is what you want, so just remove it if not needed
// array_push($month_links, '0');
// }
}
}
print_r( $month_links);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.