未定义的索引:HotelImages - Expedia

时间:2016-11-22 12:00:53

标签: php mongodb laravel

我有以下摘录,取自http://developer.ean.com/docs/hotel-info/examples/rest-default-content

[ "HotelImages": {
            "@size": "16",
            "HotelImage": [
                {
                    "hotelImageId": 4694179,
                    "name": "",
                    "category": 1,
                    "type": 0,
                    "caption": "Exterior",
                    "url": "/hotels/1000000/10000/5900/5900/5900_44_b.jpg",
                    "thumbnailUrl": "/hotels/1000000/10000/5900/5900/5900_44_t.jpg",
                    "supplierId": 13,
                    "width": 350,
                    "height": 350,
                    "byteSize": 0
                },
                {
                    "hotelImageId": 4694182,
                    "name": "",
                    "category": 2,
                    "type": 0,
                    "caption": "Lobby",
                    "url": "/hotels/1000000/10000/5900/5900/5900_47_b.jpg",
                    "thumbnailUrl": "/hotels/1000000/10000/5900/5900/5900_47_t.jpg",
                    "supplierId": 13,
                    "width": 350,
                    "height": 350,
                    "byteSize": 0
                }, ]

如何获取url的数据?它实际上是一个我们应该附加http://images.travelnow.com/

的网址

示例:echo $data['HotelImages']['HotelImage']['url'];

但它给了我以下错误

  

未定义索引:HotelImages

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

更正您的JSON sytax并尝试

 $data ='{"HotelImages": {
            "@size": "16",
            "HotelImage": [
                {
                    "hotelImageId": 4694179,
                    "name": "",
                    "category": 1,
                    "type": 0,
                    "caption": "Exterior",
                    "url": "hxxp://media.expedia.com/hotels/1000000/10000/5900/5900/5900_44_b.jpg",
                    "thumbnailUrl": "hxxp://media.expedia.com/hotels/1000000/10000/5900/5900/5900_44_t.jpg",
                    "supplierId": 13,
                    "width": 350,
                    "height": 350,
                    "byteSize": 0
                },
                {
                    "hotelImageId": 4694182,
                    "name": "",
                    "category": 2,
                    "type": 0,
                    "caption": "Lobby",
                    "url": "hxxp://media.expedia.com/hotels/1000000/10000/5900/5900/5900_47_b.jpg",
                    "thumbnailUrl": "hxxp://media.expedia.com/hotels/1000000/10000/5900/5900/5900_47_t.jpg",
                    "supplierId": 13,
                    "width": 350,
                    "height": 350,
                    "byteSize": 0
                }]}}';

    $k =  json_decode($data); 
    print_r($k->HotelImages->HotelImage[0]->url); 
    exit();