Установите переменной PHP значение в объекте JSON

#php #json

#php #json

Вопрос:

У меня есть PHP-файл, который получает объект JSON с именем $response . $response имеет много значений (как показано ниже), и я хочу извлечь несколько, включая заголовок и цену. Я пробовал приведенный ниже код, но он не показывает НИКАКОГО результата (нет ошибки, нет значения!). Мысли?

Спасибо

PHP-код:

 $data = json_decode($response, true);

echo $data->ItemsResult->Items[0]->ItemInfo->Title->DisplayValue;
  

$response =

 {
    "ItemsResult": {
        "Items": [{
            "ASIN": "B0825SNHP1",
            "BrowseNodeInfo": {
                "BrowseNodes": [{
                    "Ancestor": {
                        "Ancestor": {
                            "Ancestor": {
                                "ContextFreeName": "Toys amp; Games",
                                "DisplayName": "Toys amp; Games",
                                "Id": "165793011"
                            },
                            "ContextFreeName": "Toys amp; Games",
                            "DisplayName": "Categories",
                            "Id": "165795011"
                        },
                        "ContextFreeName": "Stuffed Animals amp; Plush Toys",
                        "DisplayName": "Stuffed Animals amp; Plush Toys",
                        "Id": "166461011"
                    },
                    "ContextFreeName": "Plush Figure Toys",
                    "DisplayName": "Plush Figures",
                    "Id": "11350121011",
                    "IsRoot": false,
                    "SalesRank": 1
                }],
                "WebsiteSalesRank": {
                    "ContextFreeName": "Toys amp; Games",
                    "DisplayName": "Toys amp; Games",
                    "SalesRank": 32
                }
            },
            "DetailPageURL": "https://www.amazon.com/dp/B0825SNHP1?tag=tpf0bee-20amp;linkCode=ogiamp;th=1amp;psc=1",
            "Images": {
                "Primary": {
                    "Large": {
                        "Height": 500,
                        "URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L.jpg",
                        "Width": 467
                    },
                    "Medium": {
                        "Height": 160,
                        "URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L._SL160_.jpg",
                        "Width": 149
                    },
                    "Small": {
                        "Height": 75,
                        "URL": "https://m.media-amazon.com/images/I/51P8jTxbP2L._SL75_.jpg",
                        "Width": 70
                    }
                },
                "Variants": [{
                    "Large": {
                        "Height": 450,
                        "URL": "https://m.media-amazon.com/images/I/51myKYEuuuL.jpg",
                        "Width": 500
                    },
                    "Medium": {
                        "Height": 144,
                        "URL": "https://m.media-amazon.com/images/I/51myKYEuuuL._SL160_.jpg",
                        "Width": 160
                    },
                    "Small": {
                        "Height": 68,
                        "URL": "https://m.media-amazon.com/images/I/51myKYEuuuL._SL75_.jpg",
                        "Width": 75
                    }
                }, {
                    "Large": {
                        "Height": 500,
                        "URL": "https://m.media-amazon.com/images/I/515j0HmJV0L.jpg",
                        "Width": 500
                    },
                    "Medium": {
                        "Height": 160,
                        "URL": "https://m.media-amazon.com/images/I/515j0HmJV0L._SL160_.jpg",
                        "Width": 160
                    },
                    "Small": {
                        "Height": 75,
                        "URL": "https://m.media-amazon.com/images/I/515j0HmJV0L._SL75_.jpg",
                        "Width": 75
                    }
                }, {
                    "Large": {
                        "Height": 466,
                        "URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L.jpg",
                        "Width": 500
                    },
                    "Medium": {
                        "Height": 149,
                        "URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L._SL160_.jpg",
                        "Width": 160
                    },
                    "Small": {
                        "Height": 70,
                        "URL": "https://m.media-amazon.com/images/I/51ARoLKEn5L._SL75_.jpg",
                        "Width": 75
                    }
                }, {
                    "Large": {
                        "Height": 500,
                        "URL": "https://m.media-amazon.com/images/I/414gFkO4AmL.jpg",
                        "Width": 446
                    },
                    "Medium": {
                        "Height": 160,
                        "URL": "https://m.media-amazon.com/images/I/414gFkO4AmL._SL160_.jpg",
                        "Width": 143
                    },
                    "Small": {
                        "Height": 75,
                        "URL": "https://m.media-amazon.com/images/I/414gFkO4AmL._SL75_.jpg",
                        "Width": 67
                    }
                }, {
                    "Large": {
                        "Height": 500,
                        "URL": "https://m.media-amazon.com/images/I/516zb5W2o3L.jpg",
                        "Width": 476
                    },
                    "Medium": {
                        "Height": 160,
                        "URL": "https://m.media-amazon.com/images/I/516zb5W2o3L._SL160_.jpg",
                        "Width": 152
                    },
                    "Small": {
                        "Height": 75,
                        "URL": "https://m.media-amazon.com/images/I/516zb5W2o3L._SL75_.jpg",
                        "Width": 71
                    }
                }]
            },
            "ItemInfo": {
                "ByLineInfo": {
                    "Brand": {
                        "DisplayValue": "Mattel",
                        "Label": "Brand",
                        "Locale": "en_US"
                    },
                    "Manufacturer": {
                        "DisplayValue": "Mattel",
                        "Label": "Manufacturer",
                        "Locale": "en_US"
                    }
                },
                "Classifications": {
                    "Binding": {
                        "DisplayValue": "Accessory",
                        "Label": "Binding",
                        "Locale": "en_US"
                    },
                    "ProductGroup": {
                        "DisplayValue": "Toy",
                        "Label": "ProductGroup",
                        "Locale": "en_US"
                    }
                },
                "ContentInfo": {
                    "Edition": {
                        "DisplayValue": "Star Wars Edition",
                        "Label": "Edition",
                        "Locale": "en_US"
                    }
                },
                "ExternalIds": {
                    "EANs": {
                        "DisplayValues": ["0887961938814"],
                        "Label": "EAN",
                        "Locale": "en_US"
                    },
                    "UPCs": {
                        "DisplayValues": ["887961938814"],
                        "Label": "UPC",
                        "Locale": "en_US"
                    }
                },
                "Features": {
                    "DisplayValues": ["This 11-inch The Child plush toy will capture the hearts of Star Wars fans everywhere", "Inspired by the Disney  series The Mandalorian, the adorable figure with green skin, big ears and large eyes resembles a baby Yoda but is referred to as The Child.", "The toy plush has a soft body, plus a sturdy base filled with beans, perfect for cuddling or display as a collectible", "The character wears his robes as seen in the show.", "Star Wars fans will love taking on the role of The Mandalorian Bounty Hunter and caring for The Child on their own", "Material Type: Polyester"],
                    "Label": "Features",
                    "Locale": "en_US"
                },
                "ManufactureInfo": {
                    "ItemPartNumber": {
                        "DisplayValue": "GWD85",
                        "Label": "PartNumber",
                        "Locale": "en_US"
                    },
                    "Model": {
                        "DisplayValue": "GWD85",
                        "Label": "Model",
                        "Locale": "en_US"
                    },
                    "Warranty": {
                        "DisplayValue": "No Warranty",
                        "Label": "Warranty",
                        "Locale": "en_US"
                    }
                },
                "ProductInfo": {
                    "Color": {
                        "DisplayValue": "Green",
                        "Label": "Color",
                        "Locale": "en_US"
                    },
                    "IsAdultProduct": {
                        "DisplayValue": false,
                        "Label": "IsAdultProduct",
                        "Locale": "en_US"
                    },
                    "ItemDimensions": {
                        "Height": {
                            "DisplayValue": 11.02,
                            "Label": "Height",
                            "Locale": "en_US",
                            "Unit": "Inches"
                        },
                        "Length": {
                            "DisplayValue": 5.98,
                            "Label": "Length",
                            "Locale": "en_US",
                            "Unit": "Inches"
                        },
                        "Weight": {
                            "DisplayValue": 0.220462262,
                            "Label": "Weight",
                            "Locale": "en_US",
                            "Unit": "Pounds"
                        },
                        "Width": {
                            "DisplayValue": 7.99,
                            "Label": "Width",
                            "Locale": "en_US",
                            "Unit": "Inches"
                        }
                    },
                    "ReleaseDate": {
                        "DisplayValue": "2020-02-12T00:00:01Z",
                        "Label": "ReleaseDate",
                        "Locale": "en_US"
                    },
                    "Size": {
                        "DisplayValue": "11 inches",
                        "Label": "Size",
                        "Locale": "en_US"
                    },
                    "UnitCount": {
                        "DisplayValue": 1,
                        "Label": "NumberOfItems",
                        "Locale": "en_US"
                    }
                },
                "Title": {
                    "DisplayValue": "Mattel Star Wars The Child Plush Toy, 11-Inch Small Yoda-Like Soft Figure from The Mandalorian, Green",
                    "Label": "Title",
                    "Locale": "en_US"
                }
            },
            "Offers": {
                "Listings": [{
                    "Availability": {
                        "Message": "In stock. Usually ships within 4 to 5 days.",
                        "MinOrderQuantity": 1,
                        "Type": "Now"
                    },
                    "Condition": {
                        "SubCondition": {
                            "Value": "New"
                        },
                        "Value": "New"
                    },
                    "DeliveryInfo": {
                        "IsAmazonFulfilled": false,
                        "IsFreeShippingEligible": false,
                        "IsPrimeEligible": false
                    },
                    "Id": "xZd+KU9rGH7fWwKPEUHMfhsQHzl3QpAN6kIllBjmOl90egwIXEDSkqIt1nqy2q90nMMKYhKCECzkZugn/hS6MNMQ0DeGGHgqoDimWML40ChnAKQi3WGnvzASkBlZn3fOYl/k7qoY/kafbujzE4UkOjHO6D2nEkcs",
                    "IsBuyBoxWinner": false,
                    "MerchantInfo": {
                        "DefaultShippingCountry": "US",
                        "FeedbackCount": 261,
                        "FeedbackRating": 3.53,
                        "Id": "A111I7FGCUO8HR",
                        "Name": "ZDeals"
                    },
                    "Price": {
                        "Amount": 27.58,
                        "Currency": "USD",
                        "DisplayAmount": "$27.58"
                    },
                    "ProgramEligibility": {
                        "IsPrimeExclusive": false,
                        "IsPrimePantry": false
                    },
                    "ViolatesMAP": false
                }],
                "Summaries": [{
                    "Condition": {
                        "Value": "Collectible"
                    },
                    "HighestPrice": {
                        "Amount": 68.94,
                        "Currency": "USD",
                        "DisplayAmount": "$68.94"
                    },
                    "LowestPrice": {
                        "Amount": 58.99,
                        "Currency": "USD",
                        "DisplayAmount": "$58.99"
                    },
                    "OfferCount": 2
                }, {
                    "Condition": {
                        "Value": "New"
                    },
                    "HighestPrice": {
                        "Amount": 58.75,
                        "Currency": "USD",
                        "DisplayAmount": "$58.75"
                    },
                    "LowestPrice": {
                        "Amount": 22.48,
                        "Currency": "USD",
                        "DisplayAmount": "$22.48"
                    },
                    "OfferCount": 224
                }]
            }
        }]
    }
}
  

Ответ №1:

Второй аргумент функции json_decode активирует результат ассоциативного массива. Итак, в вашем случае вы можете либо сделать

 $data = json_decode($response);

echo $data->ItemsResult->Items[0]->ItemInfo->Title->DisplayValue;
  

или

 $data = json_decode($response, true);

echo $data['ItemsResult']['Items'][0]['ItemInfo']['Title']['DisplayValue'];
  

Комментарии:

1. Успех!!!! Это сработало, большое вам спасибо @Михаил Просалов