php-extract data from nested json -
i've nested json code below:
fn({ "processingdurationmillis": 714, "authorisedapi": true, "success": true, "airline": "mh", "validcreditcards": [ "ax", "ca", "vi" ], "paypal": true, "outboundoptions": [ { "optionid": 0, "flights": [ { "flightnumber": "0066", "departureairport": { "code": "kul", "name": "kuala lumpur intl", "city": "kuala lumpur", "country": "malaysia", "timezone": "asia/kuala_lumpur", "lat": 2.745578, "lng": 101.709917, "terminal": null, "gate": null }, "arrivalairport": { "code": "icn", "name": "incheon intl", "city": "seoul", "country": "south korea", "timezone": "asia/seoul", "lat": 37.469075, "lng": 126.450517, "terminal": null, "gate": null }, "marketingairline": "mh", "mealindicator": "m", "allowedssrs": { }, "operatingairline": null, "equipment": "333", "equipmentname": "airbus a330-300", "flightrph": 10101, "comments": [ "mh codeshare ke" ], "depscheduled": "2015-04-28t23:30:00.000+08:00", "arrscheduled": "2015-04-29t07:10:00.000+09:00", "depestimated": null, "depactual": null, "arrestimated": null, "arractual": null, "eligibleforeticketing": true, "cabin": "economy", "faremarketingtype": "lowest", "rbd": "o", "seatsavailable": 2, "durationminutes": 400, "minutestoscheduledflightdeparture": 7877 } ], "stopovers": [ ], "faredetails": { "perpassengerjourneyfares": [ { "passengertype": "adt", "fare": "852.93", "currencycode": "myr" } ], "perpassengertriptaxes": [ { "passengertype": "adt", "totaltax": "68.90", "taxes": [ { "code": "my", "amount": "65.00", "currency": "myr" }, { "code": "d8", "amount": "3.90", "currency": "myr" } ] } ], "journeyfare": "852.93", "totaltripfare": "921.90", "farecurrency": "myr" }, "magicstring": "mbkclcd63tlngupp1zhbqe99f5uy2sahl3rnmrgd5/bylh3q2u2ufdcv5daiq/okp6vkduvbjnursihil2t/e9xtwtfosqbw18t2hpqi6kapglwndjdxl8tiz63ytq+/wrx3rsj62km5azsj6pilwebn8wvzy1b8djssmw8dwnkmcwjnxbgor0mepeewsu8ebiytohfadmcd1xdk+kczn2pmjxxuvdfgvxjnndoke71h/cw5njh2baetcqv+fqluaqfmkutzczir6hjg2u+kjslaveufpr9rnmen3vvluznble0bxe0utbehnslrbrve7wfdeofog7iiiyky42z1l4t8civy92x/vvhoe2oopons7xh8ylbg86bos1eneftqoz3umzexoxrsjl/daqoaw3hrj4mv4iisuwli69r8whbt/iw3jyegdpnsza81w4kjjl4bkamun7pd4dygdglrfkypwcyyyw+wlfge8v//d+ndyrmvzqoh9hpq7nlv8lwpikk/zzkjsjgv4jtnrnuvyp5/oxzs392p8tzlekgwuq3lsnzx3p+msvnfu3lgl+6vjrplkiac9pzef+nnryzgkp+hv8nafhzeoguxxwxkyazcfwhlnrluj894k7gbp8dwrne52ngzufxrs77datvcc1kaw4zfj920n126dp05jvrkl6toepg7mdhv1scl72p7sictor+roaykmaedi9lpebkmtja/sihb581ehz15m/tp9vfd8q62wdfgzub6fmd8/5yxk/gt4bcwocbxnqd3nva8jq2pvzu22jjfg+slgkvlxtaplqx4hyrxryujxl4kuais+uvx0tbbwdiuvpozgdeiua/eq8xbxvj8fusswcc5ut3pmykk50hu0txhv40iijp9ctclqyh13xl6dyj4n24utugrhyn42uorgkjlimtxllj8wwf7qfnmvpnlrkafqmj/80yaf/i43h3ixuuefpipc2qsaun/5ktb3hsbkhugb/cqgjlptbrpvrjjsaicmz9hedgbnwrymnq7mxrpmedehnfyl4wy8gu0n75qalxsbawehlv0runx1krk04o6p1uf20wetpgxsjpjrofpjllowar019mgfxnbvodvqbb5smdvld3jfxysw4+9kelfcf+ffzfzzdxuuwamg5p6u+hioec13mmz63hpu+kzna/0meltsgmxce919qryet1dxu4ty3u5fj5njc028nf6llpdhs7unok8aki3ydalgslt12rnqjlgoelhjtqxwx5ez1jjujalpm00sgcq4rhtsoqaoaiqhf4mq1znes3uyiwsk0mmgugxvgz1rdaq9g/e", "seatsavailable": [ 2 ], "corporateaccount": false, "flightcanbeheld": true, "durationminutes": 400, "gafaredetails": { "perpassengerjourneyfares": [ { "passengertype": "adt", "fare": "852.93", "currencycode": "myr" } ], "perpassengertriptaxes": [ { "passengertype": "adt", "totaltax": "68.90", "taxes": [ { "code": "my", "amount": "65.00", "currency": "myr" }, { "code": "d8", "amount": "3.90", "currency": "myr" } ] } ], "journeyfare": "852.93", "totaltripfare": "921.90", "farecurrency": "myr" }, "adobefaredetails": { "perpassengerjourneyfares": [ { "passengertype": "adt", "fare": "234.81", "currencycode": "usd" } ], "perpassengertriptaxes": [ { "passengertype": "adt", "totaltax": "18.97", "taxes": [ { "code": "my", "amount": "17.89", "currency": "usd" }, { "code": "d8", "amount": "1.07", "currency": "usd" } ] } ], "journeyfare": "234.81", "totaltripfare": "253.80", "farecurrency": "usd" }, "useragentfaredetails": { "perpassengerjourneyfares": [ { "passengertype": "adt", "fare": "234.81", "currencycode": "usd" } ], "perpassengertriptaxes": [ { "passengertype": "adt", "totaltax": "18.97", "taxes": [ { "code": "my", "amount": "17.89", "currency": "usd" }, { "code": "d8", "amount": "1.07", "currency": "usd" } ] } ], "journeyfare": "234.81", "totaltripfare": "253.80", "farecurrency": "usd" }, "directflight": true, "eligibleforeticketing": true, "lowestseatcount": 2 } ], "departureairport": { "code": "kul", "name": "kuala lumpur intl", "city": "kuala lumpur", "country": "malaysia", "timezone": "asia/kuala_lumpur", "lat": 2.745578, "lng": 101.709917, "terminal": null, "gate": null }, "arrivalairport": { "code": "icn", "name": "incheon intl", "city": "seoul", "country": "south korea", "timezone": "asia/seoul", "lat": 37.469075, "lng": 126.450517, "terminal": null, "gate": null }, "apirequired": true, "farerules": [ { "id": 40, "order": 0, "priority": 0, "code": "lowest", "name": "mhlow", "value": "economy class fares", "listfarerules": [ { "id": 10, "order": 0, "code": "", "name": "discount level", "value": "up 80%" }, { "id": 20, "order": 1, "code": "", "name": "where buy", "value": "all channels" }, { "id": 30, "order": 2, "code": "", "name": "advance purchase", "value": "applies" }, { "id": 40, "order": 3, "code": "", "name": "payment", "value": "ticket dateline applies" }, { "id": 50, "order": 4, "code": "", "name": "baggage allowance", "value": "2pc/30kg" }, { "id": 60, "order": 5, "code": "", "name": "advance seat selection", "value": "not allowed" }, { "id": 70, "order": 6, "code": "", "name": "enrich miles", "value": "nil" }, { "id": 80, "order": 7, "code": "", "name": "change of booking", "value": "not allowed" }, { "id": 90, "order": 8, "code": "", "name": "upgrade", "value": "not allowed" }, { "id": 100, "order": 9, "code": "", "name": "stand @ airport", "value": "not allowed" }, { "id": 110, "order": 10, "code": "", "name": "no show", "value": "penalty applies" }, { "id": 120, "order": 11, "code": "", "name": "refund", "value": "no refund" } ], "listfarenotes": [ { "id": 10, "order": 0, "code": "", "name": "important notice", "value": "" }, { "id": 15, "order": 1, "code": "", "name": "", "value": "" }, { "id": 20, "order": 2, "code": "", "name": "1.", "value": "generic attributes shown applies mh operated flights. mh3000-3999, mh5200-5999 , mh9000-9999 series flights subject own rules. please contact mh call center or ticket offices actual fare rules." }, { "id": 30, "order": 3, "code": "", "name": "2.", "value": "for transpacific , transatlantic flights, following baggage allowances apply: economy - 2 pieces (23kg each piece), first , business - 2 pieces (32kg each piece)." }, { "id": 50, "order": 5, "code": "", "name": "3.", "value": "upgrade, standby @ airport , refund fees specific routes can obtained subsequent booking pages." }, { "id": 60, "order": 6, "code": "", "name": "4.", "value": "standby @ airport denotes same day earlier flight." }, { "id": 70, "order": 7, "code": "", "name": "5.", "value": "fare rules shown indicative only. please call our contact center check detailed fare rules." }, { "id": 80, "order": 8, "code": "", "name": "", "value": "should there discrepancy between above information , terms , conditions (t&c) published in fare rules, t&c in fare rules shall prevail." } ] } ], "errors": [ ], "warnings": [ ] })
when trying flight number json above, return me error."trying property of non-object" , "invalid argument supplied foreach()".
here code:
$flight = json_decode($result,true); $info = $flight->outboundoptions->flights; foreach ($info $flightinfo){ echo $flightinfo['flightnumber']; }
i know wrong, i'm not sure if i'm on right track or not.
thanks in advance.
by specifying true
second parameter of json_decode()
telling return associative array rather object, take out match current code.
also aware $flight->outboundoptions
array, you'll need loop or access [0]
index before can ->flights
.
try this:
$flight = json_decode($result); $info = $flight->outboundoptions[0]->flights; foreach ($info $flightinfo){ echo $flightinfo->flightnumber; }