通过print_r迭代amazon购物API(大型多维数组)

时间:2011-03-27 20:36:39

标签: php arrays api multidimensional-array

我一直无法在API返回的数组中检索值,通常是因为它们非常复杂。无论如何,这个让我难过。

我不会在这里发布整个数组..但下面是它的要点。

只是为了让你知道,我已经尝试嵌套了几个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-&gt;标题部分和DetailPageURL。期待听到建议。

感谢。

1 个答案:

答案 0 :(得分: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;
}