Reputation: 463
Passing values to an array via foreach loop then in first array result was ok but in the second,third and so forth it repeats the values of before array like in the array second it repeats the value of array first, in the third array it repeats the array values of the second. How can I fix this?
Here's how I pass my values to my array:
<?php
//
$item_holder = array();
$item_feature_holder = array();
$item_shipping_holder = array();
$item_imagesets_holder = array();
//Get items from amazon
foreach($response->Items->Item as $item){
//Display item features
if(isset($item->ItemAttributes->Feature)){
if(is_array($item->ItemAttributes->Feature)){
foreach($item->ItemAttributes->Feature as $itemFeature){
echo '<p>'.$itemFeature.'</p>';
$item_feature_holder[] = $itemFeature;
}
}
else{
echo '<p>'.$item->ItemAttributes->Feature.'</p>';
}
}
//Display more images
foreach($item->ImageSets->ImageSet as $moreImage){
if(isset($moreImage->ThumbnailImage)){
$item_imagesets_holder[] = $moreImage->SmallImage->URL;
}
}
$item_shipping_holder = array(
'standard'=>$standard_shipping,
'expedited'=>$expedited_shipping,
'twoday'=>$twoday_shipping,
'oneday'=>$oneday_shipping);
$item_holder[] = array(
'default_image'=>$item_image_medium,
'category'=>$item->ItemAttributes->ProductGroup,
'title'=>$item->ItemAttributes->Title,
'asin'=>$item->ASIN,
'weight'=>$rounded_weight,
'description'=>$product_description,
'features'=>$item_feature_holder,
'shipping'=>$item_shipping_holder,
'image'=>$item_imagesets_holder);
}
In my dump the first array was ok but in my second array, in the features and images part it repeats the data from the first one.
Here's the var_dump of my array:
array (size=9)
'default_image' => string 'http://ecx.images-amazon.com/images/I/21pp4CsKwdL._SL160_.jpg' (length=61)
'category' => string 'Personal Computer' (length=17)
'title' => string 'MSI Computer Corp. Notebook Computer GE60 0NC-006US;9S7-16GA11-006 15.6-Inch Laptop' (length=83)
'asin' => string 'B007YDHLYE' (length=10)
'weight' => float 10.4
'description' => string 'MSI GE60 0NC-006US 15.6 inch Core i7-3610QM 2.3GHz/6GB DDR3/750GB HDD/DVD Super Multi/USB3.0/W7HP Notebook Computer (Black/Red)' (length=127)
'features' =>
array (size=5)
0 => string '3rd Generation Intel® Core™ i7 3610QM @2.3GHz. Intel® HM76 Chipset.' (length=71)
1 => string '6GB (1x4GB + 1x2GB) SO-DIMM RAM, Max. 16GB. 750GB 7200rpm Hard Drive (With m-SATA port)' (length=87)
2 => string '15.6" Full HD (1920x1080) LED backlight, Anti-glare screen. NVIDIA GeForce GTX650M 2GB GDDR5 with Optimus technology. 4(2Wx4)HD Speakers, THX True Studio Pro.' (length=158)
3 => string 'Keyboard by SteelSeries, 103 keys. HD Webcam (30fps@720p). Wireless LAN 802.11 b/g/n. Bluetooth 4.0' (length=99)
4 => string 'Windows 7 Home Premium 64-bit' (length=29)
'shipping' =>
array (size=4)
'standard' => string '$9.69' (length=5)
'expedited' => string '$16.79' (length=6)
'twoday' => string 'N/A' (length=3)
'oneday' => string 'N/A' (length=3)
'image' =>
array (size=5)
0 => string 'http://ecx.images-amazon.com/images/I/21pp4CsKwdL._SL75_.jpg' (length=60)
1 => string 'http://ecx.images-amazon.com/images/I/21OHSB3BiyL._SL75_.jpg' (length=60)
2 => string 'http://ecx.images-amazon.com/images/I/211kjZnCiFL._SL75_.jpg' (length=60)
3 => string 'http://ecx.images-amazon.com/images/I/21TKV3LXBmL._SL75_.jpg' (length=60)
4 => string 'http://ecx.images-amazon.com/images/I/11WG9GF3nPL._SL75_.jpg' (length=60)
array (size=9)
'default_image' => string 'http://ecx.images-amazon.com/images/I/215zEm1MX8L._SL160_.jpg' (length=61)
'category' => string 'Personal Computer' (length=17)
'title' => string 'MSI Computer Corp. Notebook Computer GE70 0ND-033US;9S7-175611-033 17.3-Inch Laptop' (length=83)
'asin' => string 'B0082JFD1W' (length=10)
'weight' => float 11.95
'description' => string 'MSI GE70 0ND-033US 17.3 inch Core i7-3610QM 2.3GHz/8GB DDR3/750GB HDD/USB 3.0/DVD+/-RW/W7HP Notebook Computer (Black)' (length=117)
'features' =>
array (size=10)
0 => string '3rd Generation Intel® Core™ i7 3610QM @2.3GHz. Intel® HM76 Chipset.' (length=71)
1 => string '6GB (1x4GB + 1x2GB) SO-DIMM RAM, Max. 16GB. 750GB 7200rpm Hard Drive (With m-SATA port)' (length=87)
2 => string '15.6" Full HD (1920x1080) LED backlight, Anti-glare screen. NVIDIA GeForce GTX650M 2GB GDDR5 with Optimus technology. 4(2Wx4)HD Speakers, THX True Studio Pro.' (length=158)
3 => string 'Keyboard by SteelSeries, 103 keys. HD Webcam (30fps@720p). Wireless LAN 802.11 b/g/n. Bluetooth 4.0' (length=99)
4 => string 'Windows 7 Home Premium 64-bit' (length=29)
5 => string 'Intel Core_i7_3610QM Processor 2.3GHz' (length=37)
6 => string '8 GB SO-DIMM RAM' (length=17)
7 => string '750GB 7200rpm Hard Drive' (length=25)
8 => string '17.3-Inch Screen, NVIDIA GeForce GTX660M' (length=40)
9 => string 'Windows 7 Home Premium 64-bit' (length=29)
'shipping' =>
array (size=4)
'standard' => string '$10.49' (length=6)
'expedited' => string '$18.37' (length=6)
'twoday' => string 'N/A' (length=3)
'oneday' => string 'N/A' (length=3)
'image' =>
array (size=10)
0 => string 'http://ecx.images-amazon.com/images/I/21pp4CsKwdL._SL75_.jpg' (length=60)
1 => string 'http://ecx.images-amazon.com/images/I/21OHSB3BiyL._SL75_.jpg' (length=60)
2 => string 'http://ecx.images-amazon.com/images/I/211kjZnCiFL._SL75_.jpg' (length=60)
3 => string 'http://ecx.images-amazon.com/images/I/21TKV3LXBmL._SL75_.jpg' (length=60)
4 => string 'http://ecx.images-amazon.com/images/I/11WG9GF3nPL._SL75_.jpg' (length=60)
5 => string 'http://ecx.images-amazon.com/images/I/215zEm1MX8L._SL75_.jpg' (length=60)
6 => string 'http://ecx.images-amazon.com/images/I/21dAgtNONpL._SL75_.jpg' (length=60)
7 => string 'http://ecx.images-amazon.com/images/I/21Buy-UDJoL._SL75_.jpg' (length=60)
8 => string 'http://ecx.images-amazon.com/images/I/11LWf2AYqeL._SL75_.jpg' (length=60)
9 => string 'http://ecx.images-amazon.com/images/I/11KuBpuL6NL._SL75_.jpg' (length=60)
Upvotes: 1
Views: 235
Reputation: 390
Your code is a sheer mess. You should rewrite it.
$item_holder = [];
$item_shipping_holder = [];
function getItemFeatures($itemAttributes)
{
if (!isset($itemAttributes->Feature))
return [];
return (array) $itemAttributes->Features;
}
function getItemImages($imageSets)
{
$result = [];
foreach($imageSets->ImageSet as $moreImage)
{
if(isset($moreImage->ThumbnailImage))
{
$result[] = $moreImage->SmallImage->URL;
}
}
return $result;
}
//Get items from amazon
foreach($response->Items->Item as $item)
{
$features = getItemFeatures($response->itemAttributes);
// Display item features.
foreach ($features as $feature)
{
echo "<p>{$feature}</p>";
}
$item_shipping_holder = [
'standard' => $standard_shipping,
'expedited' => $expedited_shipping,
'twoday' => $twoday_shipping,
'oneday' => $oneday_shipping
];
$item_holder[] = [
'default_image' => $item_image_medium,
'category' => $item->ItemAttributes->ProductGroup,
'title' => $item->ItemAttributes->Title,
'asin' => $item->ASIN,
'weight' => $rounded_weight,
'description' => $product_description,
'features' => $features,
'shipping' => $item_shipping_holder,
'image' => getItemImages($item->ImageSets)
];
}
Upvotes: 1
Reputation: 5683
Try resetting the holder arrays during each iteration of the for loop
foreach($response->Items->Item as $item){
$item_feature_holder = array();
$item_imagesets_holder = array();
// rest of your code
Upvotes: 2