0

I have array of related products and each has some websites I want to store all of them in just one array My problem is that when the foreach loop goes to other related product it will not store the other one and also I used $web[] and it will show me the right one but in two dimensional array because I initialized it as an array and then again I am inserting it into other array. the reason why I have $web= array(); is that it is part of my code that is included to other code so I should empty my array so I used this method.the code and the outputs are below:

<?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 "-------------------------";
?>

output:

-------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
-------------------------

as you see for the second related product I have 3 items but again it didn't make any differences for the array

Nickool
  • 3,414
  • 10
  • 39
  • 68

1 Answers1

0

Though your question is not exactly clear to me, but I think you want all the website id list in one single one dimensional array.

In that case you can use

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

Vivek Vaghela
  • 1,065
  • 8
  • 16
  • yes this is what I wanted and it is working thanks but still I cannot understand the reason why mine was not correct. – Nickool Feb 18 '14 at 18:05
  • + operator is more of an union kind of operator than actual merge. Check this [link](http://stackoverflow.com/questions/2140090/operator-for-array-in-php) – Vivek Vaghela Feb 18 '14 at 18:10