[英]How to create array in foreach loop?
我正在使用PHP HTML DOM Parser從另一個站點獲取數據。 首先,我在這個網站上獲得我的交易的URL,而不是我在每個交易網址上發送另一個請求以獲得評論。我想做一系列評論,以便我可以在以后對它們進行排序。 為什么我不能創建陣列?
看起來像這樣
include_once('simple_html_dom.php');
$result = array();
$html = file_get_html('http://csgolounge.com/profile?id='.$steamid);
foreach($html->find('div.tradepoll') as $trade)
{
$tradeid = $trade->find('.tradeheader')[0]->find('a')[0]->href;
$html = file_get_html('http://csgolounge.com/'.$tradeid);
foreach($html->find('div.message') as $message)
{
if($message->find('p',0)){}
else
{
$left = $message->find('.msgleft')[0];
$right = $message->find('.msgright')[0];
//information about comments
$time = trim(strip_tags_content($left->innertext));
$text = $left->find('.msgtxt')[0];
$result[$time]['time'] = $time;
$result[$time]['text'] = $text;
}
}
}
echo json_encode($result);
如果我回顯$ time或$ text我總是成功獲得數據。
我發現了什么問題。
每次調用file_get_html或str_get_html時,Simple HTML DOM Parser都不會清理DOM中的內存,因此每次完成當前DOM時都需要明確表達。
所以我添加了$ html-> clear(); 在循環結束時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.