通过foreach循环将值传递给数组然后在第一个数组结果中是正常的但是在第二个,第三个等等它重复数组之前的值,就像在数组中一样,它重复第一个数组的值,第三个数组它重复第二个数组的值。我该如何解决这个问题?
以下是我将值传递给数组的方法:
<?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);
}
在我的转储中,第一个数组没问题,但在我的第二个数组中,在功能和图像部分,它重复了第一个数据。
这是我的数组的var_dump:
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)
答案 0 :(得分:2)
尝试在for循环的每次迭代期间重置持有者数组
foreach($response->Items->Item as $item){
$item_feature_holder = array();
$item_imagesets_holder = array();
// rest of your code
答案 1 :(得分:1)
你的代码简直就是一团糟。你应该重写它。
$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)
];
}