2011-03-27 19 views
0

遍歷API返回數組中的值總是有問題,通常是因爲它們非常複雜。無論如何,這一個讓我難住。通過print_r遍歷亞馬遜購物API(大型多維數組)通過print_r

我不打算髮布整個陣列在這裏..但下面是它的要義。

爲了讓你知道,我試圖嵌套幾個foreach循環,但我得到了幾個錯誤,我無法想象有沒有更簡單的方法來做到這一點。

感謝您的幫助提前。

樣品輸出:

<? 

Array 
(
    [OperationRequest] => Array 
     (
      [HTTPHeaders] => Array 
       (
        [Header] => Array 
         (
          [Name] => UserAgent 
          [Value] => PHP-SOAP/5.2.17 
         ) 

       ) 

      [RequestId] => a2d742d5-64b7-4de7-9c3d-a4c1cd525e7e 
      [Arguments] => Array 
       (
        [Argument] => Array 
         (
          [Name] => Service 
          [Value] => AWSECommerceService 
         ) 

       ) 

      [RequestProcessingTime] => 0.382574 
     ) 

    [Items] => Array 
     (
      [Request] => Array 
       (
        [IsValid] => True 
        [ItemSearchRequest] => Array 
         (
          [Condition] => New 
          [DeliveryMethod] => Ship 
          [Keywords] => TV 
          [MerchantId] => Amazon 
          [ResponseGroup] => Small 
          [ReviewSort] => -SubmissionDate 
          [SearchIndex] => All 
         ) 

       ) 

      [TotalResults] => 1664379 
      [TotalPages] => 166438 
      [Item] => Array 
       (
        [0] => Array 
         (
          [ASIN] => B0036WT3P2 
          [DetailPageURL] => http://www.amazon.com/Samsung-LN40C630-40-Inch-1080p-Black/dp/B0036WT3P2%3FSubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0036WT3P2 
          [ItemLinks] => Array 
           (
            [ItemLink] => Array 
             (
              [0] => Array 
               (
                [Description] => Technical Details 
                [URL] => http://www.amazon.com/Samsung-LN40C630-40-Inch-1080p-Black/dp/tech-data/B0036WT3P2%3FSubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [1] => Array 
               (
                [Description] => Add To Baby Registry 
                [URL] => http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB0036WT3P2%26SubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [2] => Array 
               (
                [Description] => Add To Wedding Registry 
                [URL] => http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB0036WT3P2%26SubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [3] => Array 
               (
                [Description] => Add To Wishlist 
                [URL] => http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB0036WT3P2%26SubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [4] => Array 
               (
                [Description] => Tell A Friend 
                [URL] => http://www.amazon.com/gp/pdp/taf/B0036WT3P2%3FSubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [5] => Array 
               (
                [Description] => All Customer Reviews 
                [URL] => http://www.amazon.com/review/product/B0036WT3P2%3FSubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

              [6] => Array 
               (
                [Description] => All Offers 
                [URL] => http://www.amazon.com/gp/offer-listing/B0036WT3P2%3FSubscriptionId%3DAKIAJQJSA2B4JHSRNEXQ%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0036WT3P2 
               ) 

             ) 

           ) 

          [ItemAttributes] => Array 
           (
            [Manufacturer] => Samsung 
            [ProductGroup] => CE 
            [Title] => Samsung LN40C630 40-Inch 1080p 120 Hz LCD HDTV (Black) 
           ) 

?> 

我試圖找回ItemAttributes->標題部分和DetailPageURL。期待聽取建議。

謝謝。

回答

0

嘗試了這一點,我認爲它會做你想要什麼,雖然我沒有測試它

$amazon_array = $your_big_ass_amazon_array; // replace this with the array you show in your post 
$items = $your_big_ass_amazon_array['Items']; 
foreach($items as $item) { 
    $ItemAttributes = getSubArrayByIndex($item, 'ItemAttributes'); 
    $tittles[] = $ItemAttributes['title']; 
} 

print_r($titles); 

function getSubArrayByIndex($array, $index) { 
    if (!is_array($array)) return null; 
    if (isset($array[$index])) return $array[$index]; 
    foreach ($array as $item) { 
     $return = getSubArrayByIndex($item, $index); 
     if (!is_null($return)) { 
      return $return; 
     } 
    } 
    return null; 
} 
+0

嗯..得到一個錯誤:警告:的foreach提供了無效的參數() – psarid 2011-03-27 21:45:55

+0

對不起,我錯過閱讀我認爲itemes在OperationRequest中,但是它沒有看到我的編輯 – mcgrailm 2011-03-27 22:59:53

+0

@ user628596那爲你解決了嗎?你有沒有修改任何東西? – mcgrailm 2011-03-28 14:07:37