内爆一个数组然后移动到下一个数组

时间:2013-04-12 01:18:36

标签: php foreach implode

在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]:传递的参数无效。

1 个答案:

答案 0 :(得分:1)

你说只有第一个阵列被破坏了。嗯,好像,似乎:

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

这种代码的和平总是会破坏产品阵列的第一个元素。为什么 没有做像:

$sqlstring = implode("','", $detail);