开发者

Iterating through amazon shopping API (big multi-dimensional array) through print_r

开发者 https://www.devze.com 2023-02-20 16:21 出处:网络
I\'ve always had trouble with retrieving values in API-returned arrays, usually because they are so complex. Anyways, this one has me stumped.

I've always had trouble with retrieving values in API-returned arrays, usually because they are so complex. Anyways, this one has me stumped.

I'm not going to post the entire array here.. but below is the gist of it.

Just to let you know, I have tried nesting several foreach loops, but I am getting several errors and I can't imagine there isn't an easier way to do it.

Thanks for your help in advance.

Sample of the output:

<?

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
             开发者_Go百科                   (
                                    [Manufacturer] => Samsung
                                    [ProductGroup] => CE
                                    [Title] => Samsung LN40C630 40-Inch 1080p 120 Hz LCD HDTV (Black)
                                )

?>

I am trying to retrieve the ItemAttributes->Title part and the DetailPageURL. Looking forward to hearing suggestions.

Thanks.


try this out I think it will do what you want though I haven't tested it

$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

精彩评论

暂无评论...
验证码 换一张
取 消