Get nested JSON values [PHP] - php

I am trying to get some values from a nested JSON, but for some reason I am not getitng the values of the keys. This is my JSON
$data =
{
"currency":"USD",
"results":[
{
"itineraries":[
{
"outbound":{
"duration":"07:10",
"flights":[
{
"departs_at":"2018-09-25T22:50",
"arrives_at":"2018-09-26T11:00",
"origin":{
"airport":"EWR"
},
"destination":{
"airport":"STN"
},
"marketing_airline":"H1",
"operating_airline":"PF",
"flight_number":"1930",
"aircraft":"32S",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"T",
"seats_remaining":9
}
}
]
},
"inbound":{
"duration":"08:25",
"flights":[
{
"departs_at":"2018-09-27T17:55",
"arrives_at":"2018-09-27T21:20",
"origin":{
"airport":"STN"
},
"destination":{
"airport":"EWR"
},
"marketing_airline":"H1",
"operating_airline":"PF",
"flight_number":"1929",
"aircraft":"32S",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"T",
"seats_remaining":9
}
}
]
}
}
],
"fare":{
"total_price":"538.39",
"price_per_adult":{
"total_fare":"538.39",
"tax":"237.39"
},
"restrictions":{
"refundable":false,
"change_penalties":true
}
}
},
{
"itineraries":[
{
"outbound":{
"duration":"10:30",
"flights":[
{
"departs_at":"2018-09-25T18:55",
"arrives_at":"2018-09-26T04:55",
"origin":{
"airport":"EWR",
"terminal":"B"
},
"destination":{
"airport":"KEF"
},
"marketing_airline":"WW",
"operating_airline":"WW",
"flight_number":"104",
"aircraft":"321",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"X",
"seats_remaining":8
}
},
{
"departs_at":"2018-09-26T06:10",
"arrives_at":"2018-09-26T10:25",
"origin":{
"airport":"KEF"
},
"destination":{
"airport":"LGW",
"terminal":"S"
},
"marketing_airline":"WW",
"operating_airline":"WW",
"flight_number":"810",
"aircraft":"321",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"X",
"seats_remaining":9
}
}
]
},
"inbound":{
"duration":"11:05",
"flights":[
{
"departs_at":"2018-09-27T17:20",
"arrives_at":"2018-09-27T19:30",
"origin":{
"airport":"STN"
},
"destination":{
"airport":"KEF"
},
"marketing_airline":"WW",
"operating_airline":"WW",
"flight_number":"827",
"aircraft":"321",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"E",
"seats_remaining":9
}
},
{
"departs_at":"2018-09-27T21:10",
"arrives_at":"2018-09-27T23:25",
"origin":{
"airport":"KEF"
},
"destination":{
"airport":"JFK",
"terminal":"1"
},
"marketing_airline":"WW",
"operating_airline":"WW",
"flight_number":"109",
"aircraft":"321",
"booking_info":{
"travel_class":"ECONOMY",
"booking_code":"E",
"seats_remaining":9
}
}
]
}
}
],
"fare":{
"total_price":"544.61",
"price_per_adult":{
"total_fare":"544.61",
"tax":"353.61"
},
"restrictions":{
"refundable":false,
"change_penalties":true
}
}
}
]
}
I am trying to foreach trough the JSON and get the value from some of the keys like this
$arr = json_decode($data,true);
foreach($arr['results'] as $data) {
echo "duration: ".$data['itineraries']['outbound']['duration'].PHP_EOL.
"origin: ".$data['itineraries']['outbound']['flights']['origin'].PHP_EOL; //etc
}
However, I am not getting any errors while executing it, but I am not getting the values from the keys either.
I am trying get the values for the duration and origin keys. Any idea what I am doing wrong?
Just found my mistake. I forgot the [0]. So this fixed it.
foreach($arr['results'] as $data) {
echo "duration: ".$data['itineraries'][0]['outbound']['duration'].PHP_EOL.
"origin: ".$data['itineraries'][0]['outbound']['flights'][0]['origin']['airport'].PHP_EOL; //etc
}

Some parsed elements are within an array. And therefore you must add [0] on some spots.
This will work:
foreach ($arr['results'] as $data) {
echo "duration: " . $data['itineraries'][0]['outbound']['duration'] . PHP_EOL .
"origin: " . $data['itineraries'][0]['outbound']['flights'][0]['origin']['airport'] . PHP_EOL; //etc
}
Output:
duration: 07:10
origin: EWR
duration: 10:30
origin: EWR

Related

Getting values from meta_data

I have a problem with getting some values from Woocommerce $item object.
I'm trying to extract data from "meta_data" -> "_advanced_woo_discount_item_total_discount" -> initial_price, discounted_price and "discount_value" from "total _discount_details".
I tried with $item->get_meta_data(); but got a blank return.
{
"id":55,
"order_id":11501,
"name":"test",
"product_id":6419,
"variation_id":6421,
"quantity":1,
"tax_class":"",
"subtotal":"182.491803",
"subtotal_tax":"40.15",
"total":"182.491803",
"total_tax":"40.15",
"taxes":{
"total":{"1":"40.148197"},
"subtotal":{"1":"40.148197"}
},
"meta_data":[{
"id":457,
"key":"pa_velikost",
"value":"180cm"
},
{
"id":458,
"key":"_advanced_woo_discount_item_total_discount",
"value":
{
"initial_price":278.3,
"discounted_price":222.64000000000001,
"total_discount_details":
{
"8e39099d383d7d50a8c4fce98e59cc79":
{
"1":
{
"set_discount":0,
"bulk_discount":0,
"simple_discount":
{
"discount_type":"percentage",
"discount_value":"20",
"discount_quantity":1,
"discount_price_per_quantity":55.660000000000004,
"discount_price":55.660000000000004
}
}
}
},
"cart_discount_details":[],
"apply_as_cart_rule":["no"],
"discount_lines":
{
"non_applied":{
"quantity":0,
"discount":0,
"price":278.3,
"calculate_discount_from":278.3
},
"0":
{
"quantity":1,
"discount":55.660000000000004,
"original_price":278.3,
"discounted_price":222.64000000000001
}
},
"cart_quantity":1,
"product_id":6421,
"initial_price_based_on_tax_settings":278.3,
"discounted_price_based_on_tax_settings":222.64000000000001
}
}
]
}
Try with get_meta()
Example:
$item->get_meta('_advanced_woo_discount_item_total_discount')

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);

Shifting specific index's in array to front based on value inside

I have the following array.
{
"flow":[
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" },
{ "id":"3", "uid":"eric" },
{ "id":"4", "uid":"bryan" }
]
}
]
}
Let's say I am eric. If I am eric, I am trying to move all items with the uid of eric to the front of the tasks array.
So that the array would end looking like this:
{
"flow":[
{
"tasks":[
{ "id":"2", "uid":"eric" },
{ "id":"1", "uid":"bryan" }
]
},
{
"tasks":[
{ "id":"2", "uid":"eric" },
{ "id":"1", "uid":"bryan" }
]
},
{
"tasks":[
{ "id":"2", "uid":"eric" },
{ "id":"1", "uid":"bryan" }
]
},
{
"tasks":[
{ "id":"2", "uid":"eric" },
{ "id":"3", "uid":"eric" },
{ "id":"1", "uid":"bryan" },
{ "id":"4", "uid":"bryan" }
]
}
]
}
I've attempted to make a function to do it, but for some reason it's not working the way I intended it to. Does anyone know what I'm doing wrong?
function reorder_flow($flow, $uid)
{
foreach($flow as &$step)
{
//step is the array with tasks
$tasks = $step['tasks'];
$new_tasks = array();
foreach($tasks as $key => $t)
{
if($t['uid'] == $uid)
{
$new_tasks = $new_tasks + $t;
unset($tasks[$key]);
}
}
$step['tasks'] = $new_tasks + $step['tasks'];
}
return $flow;
}
This is a traversal/sorting problem.
$json = <<<JSON
{
"flow":[
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" }
]
},
{
"tasks":[
{ "id":"1", "uid":"bryan" },
{ "id":"2", "uid":"eric" },
{ "id":"3", "uid":"eric" },
{ "id":"4", "uid":"bryan" }
]
}
]
}
JSON;
$flow = json_decode($json, true);
array_walk($flow['flow'], function(&$flowItem, $key){
usort($flowItem['tasks'], function($a, $b){
$aIsMatch = $a['uid'] === 'eric';
$bIsMatch = $b['uid'] === 'eric';
if ($aIsMatch && $bIsMatch) {
return 0;
}
if ($aIsMatch) {
return -1;
}
if ($bIsMatch) {
return 1;
}
// Fallback sorting criteria - you could use something else or just return 0
return strcasecmp($a['uid'], $b['uid']);
});
});
echo json_encode($flow, JSON_PRETTY_PRINT);
Please try this:
function sortByUID(&$array, $uid) {
array_walk ($array, function($obj) use($uid) {
uasort($obj->tasks, function($a, $b) use($uid) {
return $a->uid == $uid ? -1 : 1;
});
});
}
sortByUID($data->flow, 'eric');
Demo: https://3v4l.org/mOhDX
I hope this will help.
notes:
+ does not work with arrays in that way
json_decode should have the second parameter set to true to get nested arrays
then try this in the foreach:
//step is the array with tasks
$new_tasks=array();
foreach($step['tasks'] as $t){
if($t['uid'] == $uid){
array_unshift($new_tasks,$t);
} else {
array_push($new_tasks,$t);
}
}
$step['tasks'] = $new_tasks;

elasticsearch-php multiple range query not working

I am using a client for elasticsearch client for elasticsearch, i am not getting the right result for a multiple range query.
The body that i use for querying:
{
"index":"locations",
"type":"portugal",
"body":{
"aggs":{
"unique":{
"aggs":{
"documents":{
"top_hits":{
"size":50
}
}
},
"terms":{
"field":"cp3"
}
}
},
"query":{
"filtered":{
"filter":{
"bool":{
"must":{
"range":{
"latitude":{
"gte":41.1373667,
"lte":41.1373767
},
"longitude":{
"gte":-8.631723,
"lte":-8.631713
}
}
}
}
}
}
}
}
}
the result:
{
"took":2,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":1,
"max_score":1,
"hits":[
{
"_index":"locations",
"_type":"portugal",
"_id":"AVTJ4I0g_vwSgXBDKO-W",
"_score":1,
"_source":{
"id":222956,
"latitude":41.0383217,
"longitude":-8.6317147
}
}
]
},
"aggregations":{
"unique":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":199,
"doc_count":1,
"documents":{
"hits":{
"total":1,
"max_score":1,
"hits":[
{
"_index":"locations",
"_type":"portugal",
"_id":"AVTJ4I0g_vwSgXBDKO-W",
"_score":1,
"_source":{
"id":222956,
"latitude":41.0383217,
"longitude":-8.6317147
}
}
]
}
}
}
]
}
}
}
as you can see the result's latitude is not inside the latitude range i gave it.
Using the Head plugin from elasticsearch to test:
my query:
{
"query":{
"bool":{
"must":[
{
"range":{
"latitude":{
"gte":"41.1363671",
"lte":"41.1363771"
}
}
},
{
"range":{
"longitude":{
"gt":"-8.6318828",
"lte":"-8.6318728"
}
}
}
],
"must_not":[
],
"should":[
]
}
},
"from":0,
"size":10,
"sort":[
],
"aggs":{
}
}
The result:
(empty), there is no records in this range.
Edit:
added the mapping for my index:
{
"locations":{
"aliases":{
},
"mappings":{
"portugal":{
"properties":{
"cp3":{
"type":"long"
},
"cp4":{
"type":"long"
},
"cpalf":{
"type":"string"
},
"id":{
"type":"long"
},
"latitude":{
"type":"double"
},
"localidade":{
"type":"string"
},
"longitude":{
"type":"double"
}
}
}
},
"settings":{
"index":{
"creation_date":"1463675755006",
"number_of_shards":"5",
"number_of_replicas":"1",
"uuid":"C9OO0ig_QyeigqSufK8_dA",
"version":{
"created":"2030199"
}
}
},
"warmers":{
}
}
}
Test with the following query :
{
"query":{
"bool":{
"must":[
{
"range":{
"latitude":{
"gte": 41.1363671,
"lte": 41.1363771
}
}
},
{
"range":{
"longitude":{
"gt": -8.6318828,
"lte": -8.6318728
}
}
}
],
"must_not":[
],
"should":[
]
}
},
"from":0,
"size":10,
"sort":[
],
"aggs":{
}
}
As longitude and latitude fields are double type the should be compare with double, but you have compared with string.

I have trouble reading a particular array in a JSON feed

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

Categories