I have trouble reading a particular array in a JSON feed - php

I use json_decode() to parse a json feed and everything works just fine until I come to the array (media) that keeps digits as key instead of letters. I have tryed everything but all I get is different errors when it comes to this array (media)
The media array contains links to images and I just need the first image in every media array really. But the array can keep random digits in different order for the image links.
But the main problem is that I can't print this key, even if I know the name of it is "10" for example.
My second problem is that I never know the digits of the first image.
I would be very grateful if I can get help with this.
My code:
$data = json_decode($json_obj);
foreach($data->events as $event) {
echo $event->eventDateTime.'<br>'; //works
echo $event->eventId.'<br>'; //works
echo $event->title->eventTitleText.'<br>'; //works
echo strtoupper($event->venue->city).'<br>'; //works
echo $event->venue->title.'<br>'; //works
echo $event->ticketing->url.'<br>'; //works
echo $event->media->10.'<br>'; // Does NOT work
}
Below is a complete json feed that contains just 1 event.
{
"meta":{
"total":117,
"locale":"en-US",
"page":1,
"rows":1
},
"events":[
{
"-----":"252975",
"modifiedUTC":"2014-08-29T17:23:21",
"locale":"en-US",
"localeAvailable":{
"1":"sv-SE"
},
"title":{
"presentedBy":null,
"headliners":"<a href=\"http:\/\/www....\/artists\/251396\/djurgarden-hockey\">Djurg\u00e5rden Hockey<\/a> - Eisb\u00e4ren Berlin",
"supporting":null,
"supportingText":null,
"tour":"Champions Hockey League",
"eventTitle":"<a href=\"http:\/\/www....\/artists\/251396\/djurgarden-hockey\">Djurg\u00e5rden Hockey<\/a> - Eisb\u00e4ren Berlin",
"eventTitleText":"Djurg\u00e5rden Hockey - Eisb\u00e4ren Berlin"
},
"age":null,
"currency":"SEK",
"currencySymbol":null,
"eventDateTime":"2014-09-05T20:05:00",
"eventDateTimeZone":"Europe\/Stockholm",
"eventDateTimeUTC":"2014-09-05T18:05:00",
"eventDateTimeISO":"2014-09-05T20:05:00+02:00",
"dateOnly":false,
"additionalDates":[
],
"doorDateTime":null,
"doorDateTimeUTC":null,
"announceDateTime":"2014-08-04T10:00:00",
"announceDateTimeTimeZone":"Europe\/Stockholm",
"announceDateTimeUTC":"2014-08-04T08:00:00",
"presaleDateTime":null,
"presaleDateTimeTimeZone":null,
"presaleDateTimeUTC":null,
"presaleEndDateTime":null,
"presaleEndDateTimeTimeZone":null,
"presaleEndDateTimeUTC":null,
"onsaleDateTime":"2014-08-06T09:00:00",
"onsaleDateTimeTimeZone":"Europe\/Stockholm",
"onsaleDateTimeUTC":"2014-08-06T07:00:00",
"ticketing":{
"statusId":1,
"status":"Buy Tickets",
"callToAction":"Get your tickets today!",
"callToActionEnabled":false,
"url":"http:\/\/www....\/events\/252975\/djurgarden-hockey-eisbaren-berlin-tickets",
"eventUrl":"http:\/\/www....\/events\/252975\/djurgarden-hockey-eisbaren-berlin-tickets",
"ticketLinkExists":true
},
"ticketPrice":"120.00 kr - 170.00 kr",
"doorPrice":null,
"bio":null,
"description":null,
"minorCategoryId1":"34",
"minorCategoryId2":null,
"minorCategoryId3":null,
"majorCategoryId1":"3",
"majorCategoryId2":null,
"majorCategoryId3":null,
"venue":{
"venueId":"101916",
"locale":"en-US",
"localeAvailable":{
"1":"en-US",
"2":"sv-SE"
},
"title":"Hovet",
"address_line":"Globentorget 2, Stockholm, Sweden 121 27",
"address":"Globentorget 2",
"city":"Stockholm",
"state":null,
"postalCode":"121 27",
"country":"Sweden",
"url":"http:\/\/www....\/venues\/101916\/hovet-stockholm-tickets",
"urlSlug":"hovet-stockholm-tickets",
"venueUrl":"http:\/\/www.globearenas.se\/archive\/pages\/18711\/hovet.aspx",
"phone1":"0771-31 00 00",
"phone2":null,
"alias":null,
"parking":null,
"boxOffice":null,
"directions":null,
"longitude":"18.0808159",
"latitude":"59.2941674",
"timezone":"Europe\/Stockholm",
"ada":null,
"keywords":[
],
"skinCode":"sga",
"currency":"SEK",
"media":{
"10":{
"media_id":"29254",
"width":318,
"height":187,
"file_name":"http:\/\/........net\/10-08172012-502e82fe803bf.jpeg"
},
"11":{
"media_id":"29256",
"width":238,
"height":140,
"file_name":"http:\/\/........net\/11-08172012-502e82fe8f5a7.jpeg"
},
"14":{
"media_id":"29258",
"width":608,
"height":252,
"file_name":"http:\/\/........net\/14-08172012-502e83abb9512.jpeg"
},
"12":{
"media_id":"29255",
"width":134,
"height":79,
"file_name":"http:\/\/........net\/12-08172012-502e82fe88c58.jpeg"
}
},
"links":[
{
"link_id":976779,
"link_title":null,
"link_type_id":"23",
"link_href":"http:\/\/www.marriott.com\/search\/submitSearch.mi?searchType=InCity&formType=InCity&marriottBrands=BR&destinationAddress.city=Stockholm&destinationAddress.stateProvince=&destinationAddress.country=SE"
}
]
},
"tour":[
],
"promoters":null,
"associations":{
"performerIds":[
"251396"
],
"headliners":[
{
"performerId":"251396",
"locale":"en-US",
"localeAvailable":{
"1":"en-US"
},
"name":"Djurg\u00e5rden Hockey",
"minorCategoryText":"Hockey",
"minorCategoryId":"34",
"minorCategory1Id":"34",
"minorCategory2Id":null,
"minorCategory3Id":null,
"minorCategoryUrl":"http:\/\/www....\/browse\/sports\/hockey",
"majorCategoryText":"Sports",
"major_category_id":"3",
"majorCategory1Id":"3",
"majorCategory2Id":null,
"majorCategory3Id":null,
"majorCategoryUrl":"http:\/\/www....\/browse\/sports",
"keywords":[
"DIF",
"Djurgarden"
],
"bio":null,
"shortBio":null,
"aliases":null,
"url":"http:\/\/www....\/artists\/251396\/djurgarden-hockey-tickets",
"urlSlug":"djurgarden-hockey-tickets",
"media":{
"7":{
"media_id":"149467",
"width":220,
"height":220,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_7_5294e45990e1f.jpg"
},
"24":{
"media_id":"149463",
"width":322,
"height":322,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_24_5294e45974caa.jpg"
},
"6":{
"media_id":"149465",
"width":238,
"height":140,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_6_5294e459867f5.jpg"
},
"5":{
"media_id":"149466",
"width":318,
"height":187,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_5_5294e4598bec3.jpg"
},
"9":{
"media_id":"149464",
"width":134,
"height":79,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_9_5294e45981552.jpg"
}
},
"events":[
],
"links":[
{
"link_id":976320,
"link_title":null,
"link_type_id":"13",
"link_href":"https:\/\/twitter.com\/difhockeyse"
},
{
"link_id":976321,
"link_title":null,
"link_type_id":"12",
"link_href":"https:\/\/www.facebook.com\/DJURGARDEN"
}
]
}
],
"supportingActs":[
]
},
"media":[
],
"relatedMedia":{
"1":{
"media_id":"149466",
"width":318,
"height":187,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_5_5294e4598bec3.jpg"
},
"2":{
"media_id":"149465",
"width":238,
"height":140,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_6_5294e459867f5.jpg"
},
"3":{
"media_id":"149467",
"width":220,
"height":220,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_7_5294e45990e1f.jpg"
},
"4":{
"media_id":"149464",
"width":134,
"height":79,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_9_5294e45981552.jpg"
},
"23":{
"media_id":"149463",
"width":322,
"height":322,
"file_name":"http:\/\/........net\/djurgarden-hockey_11-26-13_24_5294e45974caa.jpg"
},
"17":{
"media_id":0,
"width":"678",
"height":"399",
"file_name":"http:\/\/dc1tv648rdvbu.....net\/...\/bundles\/aeg...\/images\/defaults\/1\/1_678_399.png"
},
"18":{
"media_id":0,
"width":"564",
"height":"564",
"file_name":"http:\/\/dc1tv648rdvbu.....net\/...\/bundles\/aeg...\/images\/defaults\/1\/1_564_564.png"
}
},
"links":[
{
"link_id":976321,
"link_title":null,
"link_type_id":"12",
"link_href":"https:\/\/www.facebook.com\/DJURGARDEN"
},
{
"link_id":976320,
"link_title":null,
"link_type_id":"13",
"link_href":"https:\/\/twitter.com\/difhockeyse"
}
]
}
]
}

You need to use curly braces:
echo $event->media->{10}->width;
If you do not know what the properties will be called, you can iterate them in a nested loop:
foreach($event->media as $media){
echo $media->width . '<br/>';
}
To just get the 1st property, if you do not intend to iterate them, you can use reset():
$first = reset($event->media);
echo $first->width;
Live (updated) example: http://codepad.viper-7.com/IU2wCy

Related

How do i display the NESTED JSON data using php?

I coding a whole day just to display the nested Data from JSON that i got from the Api Provider but nothings gonna change. It's always error! what should i do?
Here's my code hope you help me in this problem.
Imagine my name of json file is 05-03-21.json.
and here's the JSON Data
{
"result":"OK",
"data":{
"body":{
"header":"Manila, National Capital Region, Philippines",
"query":{
"destination":{
"id":"1004070",
"value":"Manila, National Capital Region, Philippines",
"resolvedLocation":"CITY:1004070:UNKNOWN:UNKNOWN"
}
},
"searchResults":{
"totalCount":1286,
"results":[
{
"id":438619,
"name":"OYO 653 Rosas Garden Hotel",
"starRating":3,
"urls":[
],
"address":{
"streetAddress":"1140 MH Del Pilar St., Ermita",
"extendedAddress":"",
"locality":"Manila",
"postalCode":"1000",
"region":"Manila",
"countryName":"Philippines",
"countryCode":"ph",
"obfuscate":false
},
"welcomeRewards":{
"collect":true
},
"guestReviews":{
"unformattedRating":7,
"rating":"7.0",
"total":29,
"scale":10,
"badge":"good",
"badgeText":"Good"
},
"landmarks":[
{
"label":"City centre",
"distance":"2.0 km"
},
{
"label":"Manila Bay",
"distance":"7.6 km"
}
],
"geoBullets":[
],
"ratePlan":{
"price":{
"current":"P1,287",
"exactCurrent":1286.71,
"old":"P2,383",
"summary":"excluding taxes & fees"
},
"features":{
"freeCancellation":false,
"paymentPreference":false,
"noCCRequired":false
},
"type":"EC"
},
"neighbourhood":"Ermita",
"deals":{
"specialDeal":{
"dealText":"Save 46%"
},
"priceReasoning":"DRR-446"
},
"messaging":[
],
"badging":[
],
"pimmsAttributes":"DoubleStamps|D13|TESCO",
"coordinate":{
"lat":14.578154,
"lon":120.979947
},
"providerType":"LOCAL",
"supplierHotelId":6457589,
"isAlternative":false,
"optimizedThumbUrls":{
"srpDesktop":"https:\/\/thumbnails.trvl-media.com\/uQm5oEf1djKsihiqX24-TfWhriQ=\/250x140\/smart\/filters:quality(60)\/images.trvl-media.com\/hotels\/7000000\/6460000\/6457600\/6457589\/b189dd23_z.jpg"
}
},
{
"id":756613600,
"name":"Astrotel Anonas",
"starRating":3,
"urls":[
],
"address":{
"streetAddress":"960 Aurora Boulevard",
"extendedAddress":"near LRT Anonas Station",
"locality":"Quezon City",
"postalCode":"",
"region":"",
"countryName":"Philippines",
"countryCode":"ph",
"obfuscate":false
},
"welcomeRewards":{
"collect":true
},
"guestReviews":{
"unformattedRating":4.8,
"rating":"4.8",
"total":40,
"scale":10
},
"landmarks":[
{
"label":"City centre",
"distance":"10 km"
},
{
"label":"Manila Bay",
"distance":"17 km"
}
],
"geoBullets":[
],
"ratePlan":{
"price":{
"current":"P1,295",
"exactCurrent":1294.64,
"summary":"excluding taxes & fees"
},
"features":{
"freeCancellation":false,
"paymentPreference":false,
"noCCRequired":false
},
"type":"EC"
},
"neighbourhood":"Quezon City",
"deals":[
],
"messaging":[
],
"badging":[
],
"pimmsAttributes":"DoubleStamps|D13|TESCO",
"coordinate":{
"lat":14.627763,
"lon":121.06376
},
"providerType":"LOCAL",
"supplierHotelId":23612925,
"isAlternative":false,
"optimizedThumbUrls":{
"srpDesktop":"https:\/\/thumbnails.trvl-media.com\/0GhZSM5K0XRDs9aaIYn2FuUBUqk=\/250x140\/smart\/filters:quality(60)\/images.trvl-media.com\/hotels\/24000000\/23620000\/23613000\/23612925\/c83bf0b0_z.jpg"
}
}
]
},
"common":{
"pointOfSale":{
"numberSeparators":",.",
"brandName":"Hotels.com"
},
"tracking":{
"omniture":{
"s.prop33":"D=v33",
"s.prop32":"D=v43",
"s.prop74":"search result with dates | ms shown | N",
"s.products":"MULTISOURCE;6457589,LOCAL;23612925,LOCAL;23612485",
"s.eVar16":"Mob :: aApp",
"s.eVar40":"D=v0",
"s.eVar41":"PHP",
"s.eVar63":"60acf7e5-d622-4a1a-8110-a1fac036b405",
"s.eVar42":"30|1|20210404|20210405",
"s.eVar4":"|SI:anonymous|VS:returnVisitor|HCR:notApplicable|FC:notApplicable|NS:unknown|TI:notApplicable|SM:notApplicable|IR:anonymous|",
"s.eVar43":"en_PH|HCOM_PH|ph.hotels.com",
"s.eVar3":"2|0",
"s.eVar22":"5|PHP|24:438619:2382.7900:1286.7100:1|24:756613600:1294.6400:1294.6400:0|24:756599520:1294.6400:1294.6400:0|24:2052351936:1442.6200:1298.3600:0|24:454367:2407.3200:1299.9500:0",
"s.eVar2":"Manila, National Capital Region, Philippines",
"s.eVar23":"5|PHP|24:1279267456:1446.4300:1301.7900:0|24:632077728:2176.8300:1306.1000:1|24:494528:1455.3600:1309.8200:0|24:633744:1311.4800:1311.4800:0|24:1841636192:1311.4800:1311.4800:0",
"s.eVar24":"MCTC=20;TU=NA;PDID=NULL;MVT=",
"s.eVar7":"30",
"s.server":"ph.hotels.com",
"s.eVar6":"1004070",
"s.prop29":"D=v42",
"s.eVar5":"1",
"s.prop27":"1f6f9be9-a413-4f39-874c-86385cfe9627",
"s.eVar9":"Price - low to high",
"s.prop21":"0",
"s.prop20":"2",
"s.eVar69":"Mob :: aApp",
"s.currencyCode":"PHP",
"s.eVar26":"US:NY:NEWYORK:501",
"s.eVar29":"Mob :: aApp",
"s.prop9":"1286",
"s.eVar95":"Unknown",
"s.prop7":"1252",
"s.eVar31":"D=v0",
"s.eVar32":"D=v0",
"s.eVar33":"search result with dates",
"s.eVar34":"H1871:007.002,M376:152.000,M904:000.000,M3736:000.000,M4200:000.000,M4440.2,M4869:001.000,M4952:036.001,M4961:001.000,M5184:001.000,M5342:000.000,M5663:000.000,M6388:000.000,M6775:000.000,M6779:000.000,M7015.1,M7192:000.000,M7215:000.000,M7296:000.000,M7305:000.000,M7353:000.000,M7362.0,M7384:000.000,M7552:023.001,M7561:000.000,M7763:000.000,M7765.2,M7870:000.000,M7895:000.000,M8336:000.000,M8347:019.002,M8483:000.000,M8485:007.001,M8692:000.000,M8693:000.000,M8698:000.000,M8708:000.000,M8714:000.000,M8718:000.000,M8758:000.000,M8915:000.000,M8928:000.000,M8952:000.000,M8954:000.000,M8976:000.000,M8992:000.000,M9004:000.000,M9035:000.000,M9065:000.000,M9220:000.000,M9281:000.000,M9282:000.000,M9297:000.000,M9301:000.000,M9351:000.000,M9420:000.000,M9431:000.000,M9469:000.000,M9549:000.000,M9567.1,M9691:000.000,M9693:000.000,M9699:000.000,M9716:000.000,M9729:000.000,M9752:000.000,M9771:000.000,M9853:000.000,M9857:000.000,M9864:000.000,M9924:000.000,M9927:000.000,M9945:000.000,M9961:000.000,M9971:000.000,M10001:000.000,M10007:000.000,M10010:000.000,M10013:002.001,M10068:000.000,M10121:000.000,M10125:000.000,M10137:001.001,M10148:000.000,M10155:000.000,M10248:000.000,M9215:001.000",
"s.eVar13":"389242",
"s.prop19":"1",
"s.events":"event324,event319,event320",
"s.prop18":"D=v6",
"s.prop5":"389242",
"s.prop15":"1",
"s.prop3":"Manila, National Capital Region, Philippines",
"s.prop14":"D=v7",
"s.prop36":"|SI:anonymous|VS:returnVisitor|HCR:notApplicable|FC:notApplicable|NS:unknown|TI:notApplicable|SM:notApplicable|IR:anonymous|",
"s.eVar93":"aws.us-west-2.unknown",
"s.prop2":"Price - low to high"
}
}
}
}
}
}
And here's my php code.
<?php
$filepath = '05-03-21.json';
$json_string = file_get_contents($filepath);
$json_data = json_decode($json_string,true);
$json_data = json_decode($json_string,true);
foreach ($json_data as $var_1 ) {
foreach ($var_1['searchResults'] as $var_2 ) {
echo "Hotel Name: ".$var_2['name']."<br>";
echo "Ratings: ".$var_2['starRating']."<br>";
echo "Location: ".$var_2['address']['locality']."<br>";
}
}
?>
All i wanna do is to display the Data with loop something like this.
Image here
just parse the correct array with :
foreach ($json_data['data']['body']['searchResults']['results'] as $var_1 ) {
echo "Hotel Name: ".$var_1['name']."<br>";
echo "Ratings: ".$var_1['starRating']."<br>";
echo "Location: ".$var_1['address']['locality']."<br />========<br />";
}

PHP Select Data from Json

My JSON Data
{
"users": [
{
"userid": 1,
"username": "Admin",
"usermail": "admin#admin.com",
"authority": [
{
"authorityid": 1,
"authoritytext": "Admin",
"mission": [
{
"permission": "add"
},
{
"permission": "delete"
},
{
"permission": "move"
}
]
},
{
"authorityid": 1,
"authoritytext": "Super Admin",
"mission": [
{
"permission": "no add"
},
{
"permission": "no delete"
},
{
"permission": "only moderate"
}
]
}
]
}
]
}
MY PHP
foreach($result->users as $ok) {
foreach($ok->authority as $okey) {
foreach($okey->mission as $okeyokey) {
$test = $okeyokey->permission;
echo $test;
}
}
}
How to make this?
I want show parse json only authority{0} -> misson{0} show "permission" "add" please help me .
Maybe look ScreenShot >>>>>
I want filter {0}{1}{2} and select 0 -> show parse json
enter image description here
Is this what you are looking for?
<?php
$jsonData = '{
"users": [
{
"userid": 1,
"username": "Admin",
"usermail": "admin#admin.com",
"authority": [
{
"authorityid": 1,
"authoritytext": "Admin",
"mission": [
{
"permission": "add"
},
{
"permission": "delete"
},
{
"permission": "move"
}
]
},
{
"authorityid": 1,
"authoritytext": "Super Admin",
"mission": [
{
"permission": "no add"
},
{
"permission": "no delete"
},
{
"permission": "only moderate"
}
]
}
]
}
]
}';
$data = json_decode($jsonData);
echo($data->users[0]->authority[0]->mission[0]->permission);
You are getting objects and arrays confused
foreach($result->users as $currentUser){
$auths = $currentUser->authority;
foreach($auths as $currentAuth){
foreach($currentAuth->mission as $permission){
foreach($permission as $positionLabel => $permissionValue){
if($permissionValue == "add"){
// code here
}
}
}
}
}
Make sure you are using good labels. Dummy placeholders can be fine, but it makes tracking bugs really hard. I assume you want to check if they hold permission using a database, using PDO?
// array
$arr = ["this", "is", "an", "array"];
echo $arr[0] // prints out the word this.
$json = { "attribute": "value"}
echo $json->attribute // prints out the word value.
You can have arrays in JSON objects, and JSON Objects in arrays. How you access them are different.

get following nested array webservice in php using the mysql database

"data":{
"2":{
"sub_category_id":"1",
"product":[
{
"sub_category_id":"1",
"product_id":"1",
"product_name":"American"
},
{
"sub_category_id":"1",
"product_id":"34",
"product_name":"Ausi Product"
}
]
},
"3":{
"sub_category_id":"2",
"product":{
"2":{
"sub_category_id":"2",
"product_id":"2",
"product_name":"Canadian"
}
}
}
}
What i excepted result:
"data":[
"2":{
"sub_category_id":"1",
"product":[
{
"sub_category_id":"1",
"product_id":"1",
"product_name":"American"
},
{
"sub_category_id":"1",
"product_id":"34",
"product_name":"Ausi Product"
}
]
},
"3":{
"sub_category_id":"2",
"product":[
"2":{
"sub_category_id":"2",
"product_id":"2",
"product_name":"Canadian"
}
]
]
i.e. i wantel [] after data and product.
You can instruct the PHP json_decode() function to parse objects into associative arrays.
$parsedJSON = json_decode($jsonString, /* $assoc = */ true)

How to get key 'errors' of this data type in PHP

I have data type like below. But I want to get the 'errors' key and 'errors.message' value.
[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]
I have tried to store the code in variable ($get_errors) and then call 'errors' key like this. but the code is not working.
return $get_errors['errors'];
return $get_errors->errors;
Thanks.
Try to convert your JSON data to array then fetch the desired result from the array Try the following maybe it's work for you
$getError = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$array_getError = json_decode($getError,true);
var_dump($array_getError[0]["errors"]);
This is Json type. So you have to decode json. Then you can geta data from it
$json = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$data = json_decode($json,true);
echo $data[0]['errors']['message'];
DEMO
$x = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$errors = (json_decode($x)[0]->errors);

php parsing multiple json arrays

i have a json file(below) that contains multiple values, it's abit like this -
-makes
- the make
- the model
- year made
- year made
- year made
- the model
- year made
- year made
- the make
- the model
- year made
I can't figure out how to loop through each "make" one by one to get the model and year, Every way iv'e tried so far just seems to produce the make and nothing else.
the output im looking for would look like this(using the snippet below from the full json file)
am general:hummer:1998
am general:hummer:1999
acura:CL:1997
acura:CL:1998
etc..
small snippet of the json file im working with -
{
"makes": [
{
"name": "AM General",
"models": [
{
"name": "Hummer",
"years": [
{
"year": 1998
},
{
"year": 1999
}
]
}
]
},
{
"name": "Acura",
"models": [
{
"name": "CL",
"years": [
{
"year": 1997
},
{
"year": 1998
},
{
"year": 1999
}
]
},
{
"name": "ILX",
"years": [
{
"year": 2013
},
{
"year": 2014
},
{
"year": 2015
}
]
},
{
"name": "ILX Hybrid",
"years": [
{
"year": 2014
}
]
}
]
}
]
}
$data = json_decode($json);
foreach ($data->makes as $make) { // assuming you have gotten this far already...
foreach ($make->models as $model) { // just keep going
foreach ($model->years as $year) { // deeper
echo $make->name . ':' . $model->name . ':' . $year->year . '<br>';
}
}
}
You can open the file using file_get_contents and just decode it :
$file = file_get_contents('path_to_file');
$decoded = json_deconde($file, true); // produces array

Categories