1

foreach ループを介して配列に値を渡すと、最初の配列の結果は問題ありませんでしたが、2 番目、3 番目などでは配列の前の値を繰り返し、2 番目の配列では配列の値を繰り返し、3 番目の配列では繰り返します秒の配列値。どうすればこれを修正できますか?

値を配列に渡す方法は次のとおりです。

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

    }

私のダンプでは最初の配列は問題ありませんでしたが、2 番目の配列では、機能と画像の部分で最初の配列のデータが繰り返されます。

これが私の配列の 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)
4

2 に答える 2

2

forループの各反復中にホルダー配列をリセットしてみてください

foreach($response->Items->Item as $item){
  $item_feature_holder   = array();
  $item_imagesets_holder = array();
  // rest of your code
于 2012-10-25T06:13:26.273 に答える
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)
    ];
}
于 2012-10-25T07:10:41.540 に答える