1

我得到一个不一致的结果,有时我得到一个数组,有时我认为我没有得到一个数组,因为它触发了这种错误:

Fatal error: Cannot use string offset as an array in /home/loc/public_html/mysite.com/directory/home/test.php on line 25

这是我的代码:

$result = $response['Items']['Item'];
    $html = "";
    foreach( $result as $k => $v ){
        if(isset($v['MediumImage'])) {
            $image = "<img src='".$v['MediumImage']['URL']."' />";
        }else if(isset($v['LargeImage'])) {
            $image = "<img width='150px' height='150px' src='".$v['LargeImage']['URL']."' />";
        }else {
            $image = "<img src='css/no-image.jpg' />";
        }
        $html .= "<div class='container'>";
        $html .= "<div class='imgContainer'><a href='".$v['DetailPageURL']."'>".$image."</a></div>";
        $html .= "<h3 class='newaps'><a href='".$v['DetailPageURL']."'><span class='lrg bold'>".$v['ItemAttributes']['Title']."</span></a></h3>";
        if(isset($v['ItemAttributes']['Feature'])){
            $html .= "<div class='item-features'>";
            $html .= "<ul>";
            foreach( $v['ItemAttributes']['Feature'] as $feature ) {
                $html .= "<li>".$feature."</li>";
            }
            $html .= "</ul>";
            $html .= "</div>";
        } else {
            $html .= "<div class='item-features'>";
            $html .= "<span class='no-data'><a href='".$v['DetailPageURL']."'>More info on this item...</a></span>";
            $html .= "</div>";
        }
        $html .= "<div class='clear'></div>";
        if(isset($v['ItemAttributes']['ListPrice'])){
            $html .= "<div class='item-price'>Price: ".$v['ItemAttributes']['ListPrice']['FormattedPrice']." ".$v['ItemAttributes']['ListPrice']['CurrencyCode']."</div>";
        }
        $html .= "<div class='trackSalesRank' id='".$v['ASIN']."'>Track Sales Rank</div>";
        $html .= "</div>";

第 25 行是 $image = "<img src='".$v['MediumImage']['URL']."' />";

这是 的数组值$response

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

                )

            [RequestId] => 74dd6e53-5455-4852-8b19-72580b3f244d
            [Arguments] => Array
                (
                    [Argument] => Array
                        (
                            [Name] => Service
                            [Value] => AWSECommerceService
                        )

                )

            [RequestProcessingTime] => 0.319231
        )

    [Items] => Array
        (
            [Request] => Array
                (
                    [IsValid] => True
                    [ItemSearchRequest] => Array
                        (
                            [ItemPage] => 1
                            [Keywords] => B0083PWAPW
                            [ResponseGroup] => Array
                                (
                                    [0] => Images
                                    [1] => ItemAttributes
                                )

                            [SearchIndex] => All
                        )

                )

            [TotalResults] => 1
            [TotalPages] => 1
            [MoreSearchResultsUrl] => http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3DB0083PWAPW%26url%3Dsearch-alias%253Daws-amazon-aps&linkCode=sp1&tag=elson06-20&SubscriptionId=AKIAJJPPYQPVMQLOYLKQ
            [Item] => Array
                (
                    [ASIN] => B0083PWAPW
                    [ParentASIN] => B008GGCAVM
                    [DetailPageURL] => http://www.amazon.com/Kindle-Fire-HD/dp/B0083PWAPW%3FSubscriptionId%3DAKIAJJPPYQPVMQLOYLKQ%26tag%3Delson06-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0083PWAPW
                    [ItemLinks] => Array
                        (
                            [ItemLink] => Array
                                (
                                    [0] => Array
                                        (
                                            [Description] => Technical Details
                                            [URL] => http://www.amazon.com/Kindle-Fire-HD/dp/tech-data/B0083PWAPW%3FSubscriptionId%3DAKIAJJPPYQPVMQLOYLKQ%26tag%3Delson06-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW
                                        )

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

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

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

                                    [4] => Array
                                        (
                                            [Description] => Tell A Friend
                                            [URL] => http://www.amazon.com/gp/pdp/taf/B0083PWAPW%3FSubscriptionId%3DAKIAJJPPYQPVMQLOYLKQ%26tag%3Delson06-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW
                                        )

                                    [5] => Array
                                        (
                                            [Description] => All Customer Reviews
                                            [URL] => http://www.amazon.com/review/product/B0083PWAPW%3FSubscriptionId%3DAKIAJJPPYQPVMQLOYLKQ%26tag%3Delson06-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW
                                        )

                                    [6] => Array
                                        (
                                            [Description] => All Offers
                                            [URL] => http://www.amazon.com/gp/offer-listing/B0083PWAPW%3FSubscriptionId%3DAKIAJJPPYQPVMQLOYLKQ%26tag%3Delson06-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW
                                        )

                                )

                        )

                    [SmallImage] => Array
                        (
                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL75_.jpg
                            [Height] => Array
                                (
                                    [_] => 75
                                    [Units] => pixels
                                )

                            [Width] => Array
                                (
                                    [_] => 75
                                    [Units] => pixels
                                )

                        )

                    [MediumImage] => Array
                        (
                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL160_.jpg
                            [Height] => Array
                                (
                                    [_] => 160
                                    [Units] => pixels
                                )

                            [Width] => Array
                                (
                                    [_] => 160
                                    [Units] => pixels
                                )

                        )

                    [LargeImage] => Array
                        (
                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL.jpg
                            [Height] => Array
                                (
                                    [_] => 500
                                    [Units] => pixels
                                )

                            [Width] => Array
                                (
                                    [_] => 500
                                    [Units] => pixels
                                )

                        )

                    [ImageSets] => Array
                        (
                            [ImageSet] => Array
                                (
                                    [SwatchImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL30_.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 30
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 30
                                                    [Units] => pixels
                                                )

                                        )

                                    [SmallImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL75_.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 75
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 75
                                                    [Units] => pixels
                                                )

                                        )

                                    [ThumbnailImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL75_.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 75
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 75
                                                    [Units] => pixels
                                                )

                                        )

                                    [TinyImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL110_.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 110
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 110
                                                    [Units] => pixels
                                                )

                                        )

                                    [MediumImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL._SL160_.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 160
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 160
                                                    [Units] => pixels
                                                )

                                        )

                                    [LargeImage] => Array
                                        (
                                            [URL] => http://ecx.images-amazon.com/images/I/51EN7cswSYL.jpg
                                            [Height] => Array
                                                (
                                                    [_] => 500
                                                    [Units] => pixels
                                                )

                                            [Width] => Array
                                                (
                                                    [_] => 500
                                                    [Units] => pixels
                                                )

                                        )

                                    [Category] => primary
                                )

                        )

                    [ItemAttributes] => Array
                        (
                            [Binding] => Electronics
                            [Brand] => Kindle
                            [CatalogNumberList] => Array
                                (
                                    [CatalogNumberListElement] => 53-000406
                                )

                            [EAN] => 2609000005244
                            [EANList] => Array
                                (
                                    [EANListElement] => Array
                                        (
                                            [0] => 2609000005244
                                            [1] => 2609000006326
                                            [2] => 2609000009471
                                            [3] => 0848719003796
                                            [4] => 2609000009136
                                            [5] => 2609000005497
                                            [6] => 2609000006364
                                            [7] => 2720320491521
                                            [8] => 2609000006388
                                            [9] => 2609000009327
                                        )

                                )

                            [Feature] => Array
                                (
                                    [0] => Stunning 1280x800 HD display with rich color and deep contrast from any viewing angle
                                    [1] => Exclusive Dolby audio and dual driver stereo speakers for crisp, booming sound without distortion
                                    [2] => Ultra-fast Wi-Fi- dual-antenna, dual-band Wi-Fi for 35% faster downloads and streaming
                                    [3] => Over 22 million movies, TV shows, songs, magazines, books, audiobooks, and popular apps and games
                                )

                            [ItemDimensions] => Array
                                (
                                    [Height] => Array
                                        (
                                            [_] => 40
                                            [Units] => hundredths-inches
                                        )

                                    [Length] => Array
                                        (
                                            [_] => 760
                                            [Units] => hundredths-inches
                                        )

                                    [Weight] => Array
                                        (
                                            [_] => 87
                                            [Units] => hundredths-pounds
                                        )

                                    [Width] => Array
                                        (
                                            [_] => 540
                                            [Units] => hundredths-inches
                                        )

                                )

                            [Label] => Amazon Digital Services, Inc
                            [ListPrice] => Array
                                (
                                    [Amount] => 19900
                                    [CurrencyCode] => USD
                                    [FormattedPrice] => $199.00
                                )

                            [Manufacturer] => Amazon Digital Services, Inc
                            [Model] => 53-000406
                            [MPN] => 0379
                            [PackageDimensions] => Array
                                (
                                    [Height] => Array
                                        (
                                            [_] => 140
                                            [Units] => hundredths-inches
                                        )

                                    [Length] => Array
                                        (
                                            [_] => 1010
                                            [Units] => hundredths-inches
                                        )

                                    [Weight] => Array
                                        (
                                            [_] => 115
                                            [Units] => hundredths-pounds
                                        )

                                    [Width] => Array
                                        (
                                            [_] => 740
                                            [Units] => hundredths-inches
                                        )

                                )

                            [PackageQuantity] => 1
                            [PartNumber] => 0379
                            [ProductGroup] => Amazon Devices
                            [ProductTypeName] => ABIS_ELECTRONICS
                            [Publisher] => Amazon Digital Services, Inc
                            [ReleaseDate] => 2012-09-14
                            [Studio] => Amazon Digital Services, Inc
                            [Title] => Kindle Fire HD 7", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers
                            [UPC] => 848719003796
                            [UPCList] => Array
                                (
                                    [UPCListElement] => 848719003796
                                )

                        )

                )

        )

)

我知道它有点长。但是我得到的结果不一致,有时处理代码可以完美地工作,但有时会触发错误"Cannot use string offset"。为什么会这样?

PS:数组响应来自亚马逊产品广告 API,我知道这不是一个重要的细节,以防你想知道 :)

4

2 回答 2

1

对于所有产品,您从亚马逊获得的响应并不相同。有些产品有一些额外的数据,有些没有这些

例如:不同产品(T恤、手机)的产品属性会有所不同

某些产品可能有报价

(如果产品有报价,它将在'报价'['项目'] ['报价']中,所以首先你检查这个偏移量,如果它不存在,从属性标签中获取价格(价格将是在亚马逊回复中显示))

因此,在您打印或处理数据之前,请检查数组中的键是否仅在键中存在。

在你的情况下:

$result = $response['Items']['Item'];
$html = "";
foreach( $result as $k => $v ){
  if($k == "MediumImage"){
      echo $v['URL'];
  }
}

你做错了,你不会在这里得到 $v["MediumImage"] 。

于 2013-02-05T08:59:59.100 回答
0

我猜他们的反应是不一致的(就像谷歌地图 API 有时似乎是这样)。因此线

$image = "<img src='".$v['MediumImage']['URL']."' />";

...不会包含该MediumImage部分。我要做的是尝试像这样重写该行:

if (is_array($v['MediumImage']) && isset($v['MediumImage']) && isset($v['MediumImage']['URL'])) {
    $image = "<img src='".$v['MediumImage']['URL']."' />";
} else {
    // handle non-existing MediaImage URL here
}
于 2013-02-05T08:43:11.317 回答