Reputation: 83
I am struggling to get the "merchant" -> "id" from the Groupon API below, while I don't have any problems to return the discountPercent.
<?php
$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['deals'] as $results) {
$discountPercent = $results['options'][0]['discountPercent'];
$merchantId = $results['merchant'][0]->id;
echo $discountPercent.'<br>';
echo $merchantId;
}
?>
If someone could point me to the right direction.
Many Thanks,
Upvotes: 0
Views: 811
Reputation: 11
>>>This may be the Reason.....
>>>Explanation for:: $results['options'][0]['discountPercent']
In $json array you can see
[options] => Array
(
[0] => Array
(
.........
[discountPercent] => 54
.........
)
)
Here, the hierechy is
option->0(key)->discountPercent
that's why you need to use index '0';
>>>Explanation for::: $results['merchant'][0]->id
in json array you can see
[merchant] => Array
(
[id] => global-cuisine-restaurant
[uuid] => 183dd76b-a1a6-40cf-93c7-33e00f379451
[name] => Global Cuisine Restaurant
[websiteUrl] => http://www.globalcuisine.ie/
[twitterUrl] =>
[facebookUrl] =>
[ratings] =>
)
Here the hirerchy is:::
merchant->id (notice '0' is not present as any subarray index)
that's why should use
$merchantId = $results['merchant']['id'];
finally use can use code::
<?php
$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['deals'] as $key=>$results) {
$discountPercent = $results['options'][0]['discountPercent'];
$discountPercent[] = $results['options'][0]['discountPercent'];//to get all in one;
$merchantId = $results['merchant']['id'];
$merchantId[] = $results['merchant']['id'];//to get all in one
echo $discountPercent.'<br>';
echo $merchantId;
}
?>
Upvotes: 0