1

在 foreach 循环中内爆多个数组时遇到一点问题。

阵列现在看起来像这样。

Array (
[0] => Array ( [img] => /Content/ProductImages/big/9414339613250.jpg [prodtitle] => Heineken Lager 330ml Btls [unit] => 12pk [price] => [wasprice] => 26.99 [specprice] =>      ) 
[1] => Array ( [img] => /Content/ProductImages/big/7501064191367.jpg [prodtitle] => Corona Extra Beer 355ml Bottles [unit] => 12pk [price] => [wasprice] => 26.99 [specprice] => 22.99 )     
[2] => Array ( [img] => /Content/ProductImages/big/9414774095307.jpg [prodtitle] => Steinlager Lager 330ml Btls [unit] => 12pk [price] => [wasprice] => 23.99 [specprice] => 21.99 )

然而,在 foreach 循环中,它只会使第一个数组内爆循环必须进行的次数:

/Content/ProductImages/big/9414339613250.jpg','Heineken Lager 330ml Btls ','12pk ','','26.99','20.99
/Content/ProductImages/big/9414339613250.jpg','Heineken Lager 330ml Btls ','12pk ','','26.99','20.99
/Content/ProductImages/big/9414339613250.jpg','Heineken Lager 330ml Btls ','12pk ','','26.99','20.99

我希望它在每个数组中移动。数字或数组不是特定的,因为可能会添加或减去项目。

/Content/ProductImages/big/9414339613250.jpg','Heineken Lager 330ml Btls ','12pk','','26.99','20.99
/Content/ProductImages/big/7501064191367.jpg','Corona Extra Beer 355ml Bottles ','12pk ','','26.99','22.99

整个代码如下所示:

$html = file_get_html($url);

foreach($html->find('div.product-details-contents') as $content) {
    $detail['img'] = $content->find('img.product-details-image',0)->src;
    $detail['prodtitle'] = $content->find('span.title', 0)->plaintext;
    $detail['unit'] = $content->find('span.unit-size', 0)->plaintext;
    $detail['price'] = filter_var($content->find('span.price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    $detail['wasprice'] = filter_var($content->find('span.was-price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    $detail['specprice'] = filter_var($content->find('span.special-price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);

    $product[] = $detail;

    $sqlstring = implode("','", $product[0]);
    echo $sqlstring;
}
print_r($product);

此外,当$sqlstring = implode("','", $product[0]); $product[0]数量增加时,它会给出错误,例如:

警告:implode() [function.implode]:传递的参数无效。

4

1 回答 1

1

你说只有第一个数组正在内爆。好吧,看起来:

$sqlstring = implode("','", $product[0]);

这种代码的和平总是使产品数组的第一个元素内爆。为什么不这样做:

$sqlstring = implode("','", $detail);
于 2013-04-12T01:38:52.583 回答