3
我使用PHP HTML DOM解析器从其他站点获取数据。首先,我在本网站上获得了我的交易的网址,并且我在每个交易网址上发送了另一个请求以获得评论。我想要发表一系列评论,以便稍后对它们进行排序。为什么我不能创建数组?如何在foreach循环中创建数组?
它看起来像这样
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);
二HTML看起来像这样http://paste.ofcode.org/kudYfjCigYL247cftQMeSf
如果我回声$时间或$文字我总是得到的数据成功。
那么是什么问题? '$ result'是你想要的数组。不是吗?我有点困惑。 –
是的,我想在带有注释的foreach循环中创建数组。问题是,回声json_encode($结果);什么也不返回 – ToniD
如果你'var_dump($ result);'是你期望的?也许json_encode在非UTF-8编码上下降了。 – Progrock