2013-04-12 69 views
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循环中只内爆的第1个数组的次数循环已经去了:我希望它通过每个阵列移动

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

+0

我不理解你的问题非常好。你需要爆炸你有的子阵列吗?如果是的话,我想你可以拿上array_map功能看看。 – GarouDan

+0

是啊抱歉的是,是啊,我想我的爆子排列了一个又一个漂亮多了。 – DrDog

回答

1

你说,只有第一个阵列正在崩盘。那么,因为它看起来如此:

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

代码的这种安宁总是破坏产品数组的第一个元素。为什么 没有做像水木清华:

$sqlstring = implode("','", $detail); 
相关问题