2014-02-18 70 views
0

我有相關的產品陣列,每個都有一些網站,我想將它們全部存儲在一個數組中我的問題是,當foreach循環轉到其他相關產品時,它不會存儲另一個,並且我還使用$web[]和它會顯示我正確的一個,但在二維數組,因爲我初始化它作爲一個數組,然後再次我插入到其他數組。之所以我有$ web = array();是,它是被包括到其他代碼,所以我應該空我陣列所以就用這個method.the代碼的我的代碼部分和所述輸出是以下:如何在一次循環迭代後插入數組?

<?php 
echo "-------related----------<br>"; 
echo 'Productid: '.$productid."<br>"; 
$_product = Mage::getModel('catalog/product')->load($productid); 
$web= array(); 
foreach ($_product->getRelatedProducts() as $_product) 
{ 
echo 'Related Website ids for: '.$_product->getSku().'<br>';  
echo '<pre>website IDs in related:<br>'; 
echo "============="; 
$web+=$_product->getWebsiteIds(); 
echo "============="; 
print_r($_product->getWebsiteIds()); 
echo "=====inside array ===="; 
print_r($web); 
echo "<br>webcount in related:".print_r(count($web))."<br>"; 
} 
echo'<br>'; 
echo "Array OF ALL RELATED PRODUCTS:"; 

foreach($web as $key => $value) { 
    echo "<pre>"; 
    echo $key. "=>". $value; 
} 
echo "<br><br>COUNT".count($web); 
echo '</pre>'; 
echo "-------------------------"; 
?> 

輸出:

-------related---------- 
Productid: 78110 
Related Website ids for: XXXXXX 
website IDs in related: 
==========================Array 
(
    [0] => 1 
    [1] => 3 
    [2] => 4 
    [3] => 13 
    [4] => 14 
    [5] => 16 
    [6] => 17 
    [7] => 18 
    [8] => 19 
    [9] => 20 
    [10] => 21 
    [11] => 23 
    [12] => 24 
    [13] => 25 
    [14] => 26 
    [15] => 27 
    [16] => 28 
    [17] => 29 
    [18] => 30 
    [19] => 31 
    [20] => 34 
    [21] => 35 
    [22] => 36 
    [23] => 38 
    [24] => 40 
    [25] => 41 
    [26] => 46 
    [27] => 47 
    [28] => 48 
    [29] => 50 
    [30] => 51 
    [31] => 75 
) 
=====inside array ====Array 
(
    [0] => 1 
    [1] => 3 
    [2] => 4 
    [3] => 13 
    [4] => 14 
    [5] => 16 
    [6] => 17 
    [7] => 18 
    [8] => 19 
    [9] => 20 
    [10] => 21 
    [11] => 23 
    [12] => 24 
    [13] => 25 
    [14] => 26 
    [15] => 27 
    [16] => 28 
    [17] => 29 
    [18] => 30 
    [19] => 31 
    [20] => 34 
    [21] => 35 
    [22] => 36 
    [23] => 38 
    [24] => 40 
    [25] => 41 
    [26] => 46 
    [27] => 47 
    [28] => 48 
    [29] => 50 
    [30] => 51 
    [31] => 75 
) 
32 
webcount in related:1 
Related Website ids for: YYYYYY 
website IDs in related: 
==========================Array 
(
    [0] => 0 
    [1] => 50 
    [2] => 51 
) 
=====inside array ====Array 
(
    [0] => 1 
    [1] => 3 
    [2] => 4 
    [3] => 13 
    [4] => 14 
    [5] => 16 
    [6] => 17 
    [7] => 18 
    [8] => 19 
    [9] => 20 
    [10] => 21 
    [11] => 23 
    [12] => 24 
    [13] => 25 
    [14] => 26 
    [15] => 27 
    [16] => 28 
    [17] => 29 
    [18] => 30 
    [19] => 31 
    [20] => 34 
    [21] => 35 
    [22] => 36 
    [23] => 38 
    [24] => 40 
    [25] => 41 
    [26] => 46 
    [27] => 47 
    [28] => 48 
    [29] => 50 
    [30] => 51 
    [31] => 75 
) 
32 
webcount in related:1 

Array OF ALL RELATED PRODUCTS: 
0=>1 
1=>3 
2=>4 
3=>13 
4=>14 
5=>16 
6=>17 
7=>18 
8=>19 
9=>20 
10=>21 
11=>23 
12=>24 
13=>25 
14=>26 
15=>27 
16=>28 
17=>29 
18=>30 
19=>31 
20=>34 
21=>35 
22=>36 
23=>38 
24=>40 
25=>41 
26=>46 
27=>47 
28=>48 
29=>50 
30=>51 
31=>75 

COUNT32 
------------------------- 

正如你所看到的第二個相關產品,我有3個項目,但它再次沒有任何差異的陣列

回答

0

雖然你的問題是不完全清楚,但我想你想所有的網站id列表在一個單個一維數組。

在這種情況下,你可以使用

$web = array_merge($web, $_product->getWebsiteIds());

+0

是的,這就是我想要的,這是工作的感謝,但是我就是不明白爲什麼我的是不正確的。 – Nickool

+0

+運算符更像是一種聯盟類型的運算符,而不是實際的合併。檢查這[鏈接](http://stackoverflow.com/questions/2140090/operator-for-array-in-php) –