在php中,带有数组的foreach循环似乎写错了,但我';I’我不确定


In php my foreach loop with arrays seems to be written wrong but I'm not sure?

使用php,我试图获取网页上列出的所有东西的价格。有人能帮我把这个写对吗?我很确定我的foreach行写错了,因为这与$listing有关?此外,我不确定长部分的书写方式是否正确,即将名称和价格放入$name和$price变量中?

<?php
$all = file_get_contents('http://shop.hobbylobby.com/search/?keyword=cricut%20cartridge&match=AND&F_Sort=2&F_ALL=Y');
echo $all;
$name = array();
$price = array();
foreach($all as $listing) {
$name[] = $listing.GetElementsByClassName("listingWrpr").GetElementsByTagName("h3").innerhtml;
$price[] = $listing.GetElementsByClassName("listingWrpr").GetElementsByClassName("item price").innerhtml;
}
print_r($name);
print_r($price);
?>

您必须使用以下任何一种替代方案。

  1. 简单的html-dom解析器
  2. PHPquery
  3. 窥探

这些是PHP脚本,它将对表行进行计数,并且您可以将其作为jQuery函数进行操作。

我将在简单的html-dom解析器的帮助下为您举一个例子。

 include("../simplehtml/simple_html_dom.php");

$html = file_get_html("YOUR URL");
foreach ($html->find('.logo') as $e) {
        $obj = str_get_html($e);
        foreach ($obj->find('img') as $ea) {
            $logoImage =  $e->innertext;
        }
    }

希望这能有所帮助。