How to read Specific value from curl Response - php

this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }

assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);

Related

json decode won't work on string with array

I'm trying to import something from an .xml file that I have no control over. Everything went fine until the content:encoded part.
I'm getting the xml like this to strip it of the CDATA Tags:
$xml_object = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
The looks like this, I'm trying to get rid of all the tags and just get the "contents.body" parts, but when I try to use json_decode($itemJson); it returns null. What should I do?
[{
"id": 34543,
"type": "0",
"order": 0,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 345654,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://link.com/1567819/98/eipej334.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 2131656668,
"type": "0",
"order": 1,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 1650030480,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://links.com/jf/819/156722819/34/343.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 1740427509,
"type": "0",
"order": 2,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 802708308,
"order": 0,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p>Lorem ipsum dolor sit amet.</p>"
}, {
"type": "html",
"value": "false"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818
}]
}, {
"id": 79506641,
"type": "0",
"order": 3,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 28476702,
"order": 0,
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p><span style="\
"font-weight:"
bold;\
"="
"><br></span></p><span style="\
"font-weight:"
bold;\
"="
">lorem upisn? </span>"
}, {
"type": "html",
"value": "false"
}]
}]
}]
}]

How to JSON with duplicate 'id' values?

I am receiving data from another site with json API.
how can I get the data of the objects with the same id number.
I try as follows but it turns blank..
there are 10 objects with the same id number
how can i get them.
The typeid number is the same but the specialvalue value is different.
{"id":4620041,"active":true,"typeid":56,"specialvalue":"0.5","fields":{"magic":{"active":true,"type":"magic","value":"1.05"},"xmagic":{"active":true,"type":"xmagic","value":"7.5"}}}
{"id":4620045,"active":true,"typeid":56,"specialvalue":"1.5","fields":{"magic":{"active":true,"type":"magic","value":"3.05"},"xmagic":{"active":true,"type":"xmagic","value":"3.5"}}}
if($test->typeid =="56")
{
if (specialvalue == "0.5") {
$alta = $test->fields->magic->value;
$uste = $test->fields->xmagic->value;
}
}
echo $alta - $uste ;
Thank you in advance for your help
https://i.hizliresim.com/Pl0LL7.jpg
Few observations :
Provided JSON is not a valid JSON.
It should be an array of objects to filter the specific data as per the requirement.
Solution :
let jsonObj = [{
"id": 5246752,
"active": true,
"typeid": 56,
"specialvalue": "0.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "1.06"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "7.0"
}
}
}, {
"id": 5246753,
"active": true,
"typeid": 56,
"specialvalue": "1.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "1.35"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "3.0"
}
}
}, {
"id": 5246754,
"active": true,
"typeid": 56,
"specialvalue": "2.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "2.0"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.7"
}
}
}, {
"id": 5246755,
"active": true,
"typeid": 56,
"specialvalue": "3.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "3.45"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.25"
}
}
}, {
"id": 5246756,
"active": true,
"typeid": 56,
"specialvalue": "4.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "6.25"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.08"
}
}
}, {
"id": 5246757,
"active": true,
"typeid": 56,
"specialvalue": "5.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "9.5"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.02"
}
}
}];
let filteredData = jsonObj.filter(item => item.typeid===56 && item.specialvalue==="0.5");
console.log(filteredData);
Use .filter
var filteredData = data.filter(
(el) => {
return el.typeid===56 && el.specialvalue==="0.5";
}
);
This will return only those elements with el.typeid===56 && specialvalue==="0.5"

Display JSON Array as a list in php

I'm trying to display a JSON Array in php
JSON:
"members":[
{
"username":"Abstract",
"status":"online",
"bot":true,
"nick":"ViralBot \ud83d\ude0b",
"avatar_url":"https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar":"59b492151d2c352f3ac5e249e4f04a82",
"discriminator":"0612",
"id":"133718676741292033"
},
{
"username":"Aethex",
"status":"online",
"bot":true,
"game":{
"name":"aethex.xyz | -help"
},
"avatar_url":"https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar":"bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator":"0394",
"id":"150300454708838401"
},
{
"username":"AIRHORN SOLUTIONS",
"status":"online",
"bot":true,
"nick":"MLG AIRHORNZ!",
"game":{
"name":"airhornbot.com"
},
"avatar_url":"https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar":"96be9927ca947a75021db568485612fc",
"discriminator":"6723",
"id":"159800228088774656"
},
{
"username":"BuddyGang",
"status":"online",
"game":{
"name":"Custom Widgets API"
},
"avatar_url":"https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar":"089e84734ea5ca86d20292abcca0982f",
"discriminator":"1704",
"id":"97172171259904000"
},
{
"username":"danclay",
"status":"dnd",
"nick":"danclay \ud83d\ude09",
"avatar_url":"https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar":"fa460980203d7838c8481c93c0361f6d",
"discriminator":"2051",
"id":"97147476531757056"
},
{
"username":"dragongod100",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar":"5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator":"5291",
"id":"96716994136514560"
},
{
"username":"Hax0nWax0ff",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar":"ccf68e818fa44e342f60030a89a0fd53",
"discriminator":"7691",
"id":"125738028793593856"
},
{
"username":"iiPsionic",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar":"ba4bba83436f227933004694b4c184f2",
"discriminator":"6034",
"id":"142014347038818304"
},
{
"username":"RH1-N0",
"status":"online",
"bot":true,
"nick":"RHINO ATTACK!",
"avatar_url":"https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar":"fe0070998d6c9378edf8243e3267f950",
"discriminator":"6993",
"id":"135288293548883969"
},
{
"username":"WildBot",
"status":"online",
"bot":true,
"nick":"Cra-Cra Bot \ud83d\ude31",
"avatar_url":"https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar":"f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator":"3942",
"id":"110462073074388992"
}
]
I want to be able to display it in numeric order of the "position" variable, with the avatar positioned next to the username then the game text.
HTML layout:
<img src="AVATAR_URL" width="50%"> USERNAME - <small>GAME</small><br>
try this code bro, hope it helps
$arr=json_decode($data,true);
// to prevent error undefined index
if(isset($arr['members'])) {
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
}
Here the code:-
$data='{
"members": [
{
"username": "Abstract",
"status": "online",
"bot": true,
"nick": "ViralBot 😋",
"avatar_url": "https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar": "59b492151d2c352f3ac5e249e4f04a82",
"discriminator": "0612",
"id": "133718676741292033"
},
{
"username": "Aethex",
"status": "online",
"bot": true,
"game": {
"name": "aethex.xyz | -help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar": "bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator": "0394",
"id": "150300454708838401"
},
{
"username": "AIRHORN SOLUTIONS",
"status": "online",
"bot": true,
"nick": "MLG AIRHORNZ!",
"game": {
"name": "airhornbot.com"
},
"avatar_url": "https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar": "96be9927ca947a75021db568485612fc",
"discriminator": "6723",
"id": "159800228088774656"
},
{
"username": "BuddyGang",
"status": "online",
"game": {
"name": "Custom Widgets API"
},
"avatar_url": "https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar": "089e84734ea5ca86d20292abcca0982f",
"discriminator": "1704",
"id": "97172171259904000"
},
{
"username": "danclay",
"status": "dnd",
"nick": "danclay 😉",
"avatar_url": "https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar": "fa460980203d7838c8481c93c0361f6d",
"discriminator": "2051",
"id": "97147476531757056"
},
{
"username": "dragongod100",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar": "5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator": "5291",
"id": "96716994136514560"
},
{
"username": "Hax0nWax0ff",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar": "ccf68e818fa44e342f60030a89a0fd53",
"discriminator": "7691",
"id": "125738028793593856"
},
{
"username": "iiPsionic",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar": "ba4bba83436f227933004694b4c184f2",
"discriminator": "6034",
"id": "142014347038818304"
},
{
"username": "RH1-N0",
"status": "online",
"bot": true,
"nick": "RHINO ATTACK!",
"avatar_url": "https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar": "fe0070998d6c9378edf8243e3267f950",
"discriminator": "6993",
"id": "135288293548883969"
},
{
"username": "WildBot",
"status": "online",
"bot": true,
"nick": "Cra-Cra Bot 😱",
"avatar_url": "https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar": "f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator": "3942",
"id": "110462073074388992"
}
]
}';
$arr=json_decode($data,true);
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}

Laravel json response from Controller and use Ajax to render in the View

Im working with Laravel 5.1 and have a small issue.
I am sending a JSON response from Instagram's API for most popular media from my controller and want to grab that response in my view with ajax and show it on page to the user without loading the entire page.
I am trying to grab the username and display it in my view to the user.
Right now nothing happens and the console does not show anything, it is empty.
This is my code:
CONROLLER:
public function PopularPics(City $city){
try{
$jsonData = $city->getMostPopularPics();
return response()->json($jsonData);
}catch(\Exception $ex){
return $ex->getCode();
}
}
VIEW
<ul id="theData">
</ul>
<script>
$(function(){
$("#JRequest").click(function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: "popular",
dataType:"json",
success:function(items){
$.each(items.data,function(i,item){
console.log(item.user.username);
});
}
});
});
});
</script>
INSTAGRAM JSON
{
"data": [{
"type": "image",
"users_in_photo": [],
"filter": "Gotham",
"tags": [],
"comments": { ... },
"caption": {
"created_time": "1296656006",
"text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)",
"from": {
"username": "cocomiin",
"full_name": "",
"type": "user",
"id": "1127272"
},
"id": "26329105"
},
"likes": {
"count": 35,
"data": [{
"username": "mikeyk",
"full_name": "Kevin S",
"id": "4",
"profile_picture": "..."
}, {...subset of likers...}]
},
"link": "http://instagr.am/p/BV5v_/",
"user": {
"username": "cocomiin",
"full_name": "Cocomiin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg",
"id": "1127272"
},
"created_time": "1296655883",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",
"width": 612,
"height": 612
}
},
"id": "22518783",
"location": null
},
{
"type": "video",
"videos": {
"low_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
"width": 640,
"height": 640
},
"users_in_photo": null,
"filter": "Vesper",
"tags": [],
"comments": {
"data": [{
"created_time": "1279332030",
"text": "Love the sign here",
"from": {
"username": "mikeyk",
"full_name": "Mikey Krieger",
"id": "4",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
},
"id": "8"
},
{
"created_time": "1279341004",
"text": "Chilako taco",
"from": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"id": "3"
}],
"count": 2
},
"caption": null,
"likes": {
"count": 1,
"data": [{
"username": "mikeyk",
"full_name": "Mikeyk",
"id": "4",
"profile_picture": "..."
}]
},
"link": "http://instagr.am/p/D/",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "...",
"id": "3"
},
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
"width": 612,
"height": 612
}
},
"id": "3",
"location": null
},
...]
}

extract data value from nested JSON

I am trying to get some specific fields out of this Json.
fn({
"processingDurationMillis": 454,
"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 WITH 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": "BASIC",
"rbd": "N",
"seatsAvailable": 9,
"durationMinutes": 400,
"minutesToScheduledFlightDeparture": 6486
}
],
"stopOvers": [
],
"fareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "1214.95",
"currencyCode": "MYR"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "68.90",
"taxes": [
{
"code": "MY",
"amount": "65.00",
"currency": "MYR"
},
{
"code": "D8",
"amount": "3.90",
"currency": "MYR"
}
]
}
],
"journeyFare": "1214.95",
"totalTripFare": "1283.90",
"fareCurrency": "MYR"
},
"magicString": "2t2qi8oNXWkrDR75zYDrk9+3wNaJBzHyK1ftoR/VZPVgHO+EFTkh8DMg5WUl1ap7VjwBsnhD2gFxAwBbHhY0+k0lp7BUvSoYSKg6S6u4ZkvbIWMktl+lHgcKl46vht9//2dZVJvH4D7WJvnJTtK5O4TWNrkiTmEdHp55yRmjwWfsgNswOIMXoWrZj3OUJ4DH4POJ8rmfilimvtpBCdxNsqoZDVC9d6/6LiICZ3wHZJ7w/88QuExFV7OsHbc+jI3trRzDCCb6Ns62MGyfsXX6Pz8mJe6gs02UjapVSPa3M9CqLGMCN0xCF28WNbavhSI9jG3cWsQbxGU8rnhmjx00Iw5v2qqjdE/Dx432Qzs4s36SqUjLF7KN9hAJoQuMX3emE4gZ+7ANJ5bDTDEYZlnUZ4iXKykzUptYDyGay0evu1kdCjxPJlgiEtOl3hFMaKC+eoTsjps4RoYy0Z7oD3aP52qCYPdCH+8XTic522UKU1mW9HMjmGxH5zrvYK2rOgzSR2+xH5K3IpXHBAQqWOTEvmirP4qvg5VOPjyO9mIM83I6aY1JAkqo9jYqtEwrGqANdhA9z78EdoyQYKZBXcLsQMKz06fAczwk/WxxIi1ctL8EW+aZYddkbPo7xD6NWc8bJ+ARw5AlS1tirVNcO3mN5jVr/a6qftVuaz/0q83VsX4ztQpgMjDkptbw9Zz6DNLgiLJEzdf7fraoVUyzeth5wucOMzpLBP+ERbD7XFnDSKN8QzG6lLpDK8qy95K5FMmcF4uDq8Y1waTyIN9sS+v50OTbjr7Ebs3uKIxMZFfGUfp7YpDiVyo+2x4La4K7rhHPtoR6iEfVCjnTAUvamQu3qgL3vuSCPPPJiHFbdOrKVlp3kfAxaIcJpX3Z+Twx2cNAhsGHSk2ZazzvP5Pw1EF066VcoDkld9Oe/Qu5cC+DtG2LHhMA7NU8hMD66q9UCsXC6P/mjbKr7hatjHyyklDIKuxxirMpYkukEa73RJlhKmC0fjj4EYcgRy5MtybexuN59KaTeSEFxMGFIkv0zHp5jO/wHUvyypqbxTKFR3VAx6WpmSNg/Iui2uXDhNu/F4zJnYQUW9EyluZEPebFk2Uj455O2+y0UmFe4WnUY+0d92obZNv855/ctA4UC/LQn2s9azqdhDIeUUHuHEn2a4Grb+7l8wuai6ybBmmE62ck+CqMou+A+CUwk71KMkh3ZXf8BdeelW8Ia7r9ja7wKNBklgYo4Q8xOR63QhyCt2BiiR9aOxiDIKiW7bxSFCBga7yIPWx/NZdGjUYTuiJ9KZ7W2dKLhF6XDU5mWOV7XwMRzkyschEnjSzQWGjTTftEIiNI1V1M2bhFwc92JkfVFxwXCg==",
"seatsAvailable": [
9
],
"corporateAccount": false,
"flightCanBeHeld": true,
"durationMinutes": 400,
"gaFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "1214.95",
"currencyCode": "MYR"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "68.90",
"taxes": [
{
"code": "MY",
"amount": "65.00",
"currency": "MYR"
},
{
"code": "D8",
"amount": "3.90",
"currency": "MYR"
}
]
}
],
"journeyFare": "1214.95",
"totalTripFare": "1283.90",
"fareCurrency": "MYR"
},
"adobeFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "336.66",
"currencyCode": "USD"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "19.09",
"taxes": [
{
"code": "MY",
"amount": "18.01",
"currency": "USD"
},
{
"code": "D8",
"amount": "1.08",
"currency": "USD"
}
]
}
],
"journeyFare": "336.66",
"totalTripFare": "355.77",
"fareCurrency": "USD"
},
"userAgentFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "336.66",
"currencyCode": "USD"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "19.09",
"taxes": [
{
"code": "MY",
"amount": "18.01",
"currency": "USD"
},
{
"code": "D8",
"amount": "1.08",
"currency": "USD"
}
]
}
],
"journeyFare": "336.66",
"totalTripFare": "355.77",
"fareCurrency": "USD"
},
"eligibleForeTicketing": true,
"lowestSeatCount": 9,
"directFlight": true
}
],
"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": 50,
"order": 1,
"priority": 0,
"code": "Basic",
"name": "MHbasic",
"value": "Economy Class Fares",
"listFareRules": [
{
"id": 130,
"order": 0,
"code": "",
"name": "Discount level",
"value": "Up to 65%"
},
{
"id": 140,
"order": 1,
"code": "",
"name": "Where to buy",
"value": "All channels"
},
{
"id": 150,
"order": 2,
"code": "",
"name": "Advance purchase",
"value": "Applies"
},
{
"id": 160,
"order": 3,
"code": "",
"name": "Payment",
"value": "Ticket dateline applies"
},
{
"id": 170,
"order": 4,
"code": "",
"name": "Baggage allowance",
"value": "2pc/30kg"
},
{
"id": 180,
"order": 5,
"code": "",
"name": "Advance seat selection",
"value": "Not allowed"
},
{
"id": 190,
"order": 6,
"code": "",
"name": "Enrich miles",
"value": "Nil"
},
{
"id": 200,
"order": 7,
"code": "",
"name": "Change of booking",
"value": "Not allowed"
},
{
"id": 210,
"order": 8,
"code": "",
"name": "Upgrade",
"value": "Not allowed"
},
{
"id": 220,
"order": 9,
"code": "",
"name": "Stand by at the airport",
"value": "For a fee"
},
{
"id": 220,
"order": 10,
"code": "",
"name": "No show",
"value": "Penalty applies"
},
{
"id": 230,
"order": 11,
"code": "",
"name": "Refund",
"value": "For a fee"
}
],
"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 only applies to MH operated flights. MH3000-3999, MH5200-5999 and MH9000-9999 Series flights are subject to their own rules. Please contact MH Call Center or ticket offices for actual fare rules."
},
{
"id": 30,
"order": 3,
"code": "",
"name": "2.",
"value": "For transpacific and transatlantic flights, the following baggage allowances apply: Economy - 2 pieces (23kg each piece), First and Business - 2 pieces (32kg each piece)."
},
{
"id": 50,
"order": 5,
"code": "",
"name": "3.",
"value": "Upgrade, standby at the airport and refund fees for specific routes can be obtained from subsequent booking pages."
},
{
"id": 60,
"order": 6,
"code": "",
"name": "4.",
"value": "Standby at the airport denotes the same day for an earlier flight."
},
{
"id": 70,
"order": 7,
"code": "",
"name": "5.",
"value": "Fare rules shown are indicative only. Please call our Contact Center to check the detailed fare rules."
},
{
"id": 80,
"order": 8,
"code": "",
"name": "",
"value": "Should there be any discrepancy between the above information and the terms and conditions (T&C) published in the fare rules, then the T&C in the fare rules shall prevail."
}
]
}
],
"Errors": [
],
"Warnings": [
]
})
i want extract flight number, depScheduled, arrScheduled and journey fare from the above json.
here are my code:
$json2 = json_decode($json,true);
$result= array();
foreach ($json2['outboundOptions']['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
print_r($result);
The code above return me a error, "Invalid argument supplied for foreach()".I searched around, but still have not found the solution yet..
It is giving you that error because outboundOptions is an array of objects. What you want is to access the first object:
foreach ($json2['outboundOptions'][0]['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
Also, remove the trailing comma (,) from your ] at the end as that causes invalid json.
You can check if your json is valid by going to jsonlint.com
Working Example
Update as per your comment
To get all the flights, change your foreach loop to this:
foreach ($json3['outboundOptions'] as $flight) {
foreach($flight['flights'] as $theentity) {
$result[] = $theentity['flightNumber'];
}
}
Example
remove the trailing ',' near the very end of your json.
change your code, add a [0] before ['flights']
$json2 = json_decode($json,TRUE);
$result= array();
foreach ($json2['outboundOptions'][0]['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
print_r($result);

Categories