массивы — Json PHP разбор Zomato

Я использую zomato api, чтобы получить вывод json, который работает через php, однако я хочу распечатать несколько массивов из всех данных, которые я получаю, и у меня возникают трудности с этим. Может кто-нибудь помочь, пожалуйста. Это вывод, который я получаю

Array
(
[R] => Array
(
[res_id] => 18559961
)

[apikey] => 123
[id] => 18559961
[name] => Between Buns
[url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[location] => Array
(
[address] => C13, SDA Market, Opposite IIT Delhi, SDA, New Delhi
[locality] => SDA Market, SDA
[city] => New Delhi
[city_id] => 1
[latitude] => 28.5465059222
[longitude] => 77.1969081089
[zipcode] =>
[country_id] => 1
[locality_verbose] => SDA Market, SDA, New Delhi
)

[switch_to_order_menu] => 0
[cuisines] => Burger, American, Desserts, Sandwich, Salad, European
[average_cost_for_two] => 1500
[price_range] => 3
[currency] => Rs.
[offers] => Array
(
)

[opentable_support] => 0
[is_zomato_book_res] => 0
[mezzo_provider] => OTHER
[is_book_form_web_view] => 0
[book_form_web_view_url] =>
[book_again_url] =>
[thumb] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A
[user_rating] => Array
(
[aggregate_rating] => 4.0
[rating_text] => Very Good
[rating_color] => 5BA829
[votes] => 723
)

[photos_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop
[menu_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop
[featured_image] => https://b.zmtcdn.com/data/pictures/chains/1/18559961/c0e491e63d2e95c0c3049de0e8bbb05c.jpg?output-format=webp
[medio_provider] => 1
[has_online_delivery] => 0
[is_delivering_now] => 0
[include_bogo_offers] => 1
[deeplink] => zomato://restaurant/18559961
[is_table_reservation_supported] => 1
[has_table_booking] => 1
[book_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/book?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
[events_url] => https://www.zomato.com/ncr/between-buns-sda-new-delhi/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1
)

и это мой код

<?php

// Errors on
error_reporting(E_ALL);

// Get cURL resource
$curl = curl_init();

// Curl options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Accept: application/json', 'user-key: 123'],
CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/restaurant?res_id=123',
));

// Send the request & save response to $resp
$resp = curl_exec($curl);

// Check for errors if curl_exec fails
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

// Close request to clear up some resources
curl_close($curl);

// dumping $resp
//echo 'var_dump: <br>';
//var_dump($resp);
//echo '<br><br>';

// Decode json
$jsonZomato = json_decode($resp,true);

// print readable results
print "<pre>";
print_r($jsonZomato);
print "</pre>";
print $jsonZomato->aggregate_rating;//this is the problem

?>

Я получаю сообщение об ошибке: При попытке получить свойство необъекта в /home/betweenbuns/public_html/rating_2.php в строке 45

0

Решение

Когда вы передаете 2-й аргумент true в json_decode() функция, он возвращает массив.
Как вы сделали в этой строке:

// Decode json
$jsonZomato = json_decode($resp,true);

Итак, доступ к нему с ключами в виде массива:

print $jsonZomato['user_rating']['aggregate_rating'];//This should work
2

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]