php stdClass object Nested object for child values - php

I am working on stdClass Object in laravel. I have to combine values from two table. i.e. first one containing some questions and other one is containing their multiple choice answers. How to combine the result in a way that the questions object could contain its answer object as nested object. I want the structure like that:
stdClass Object
(
[question_id] => 4
[title] => How to add data?
[options] => Array
(
[0] => stdClass Object
(
[id] => 1
[question_id] => 4
[option_title] => By any device
)
[1] => stdClass Object
(
[id] => 2
[question_id] => 4
[option_title] => By mobile
)
[2] => stdClass Object
(
[id] => 3
[question_id] => 4
[option_title] => By Laptop
)
)
stdClass Object
(
[question_id] => 5
[title] => Where to add data?
[options] => Array
(
[0] => stdClass Object
(
[id] => 1
[question_id] => 5
[option_title] => Lahore
)
[1] => stdClass Object
(
[id] => 2
[question_id] => 5
[option_title] => Karachi
)
[2] => stdClass Object
(
[id] => 3
[question_id] => 5
[option_title] => Multan
)
);

It was little edgy using foreach with keys. Finally developed following code and it worked perfectly:
foreach ($question_data as $key => $question) {
foreach ($option_data as $key2 => $opt) {
if ($question_data[$key]->question_id == $option_data[$key2]['question_id'] && $question->q_type != 3) {
$question->options[] = [
'question_id' => $option_data[$key2]['question_id'],
'option_id' => $option_data[$key2]['id'],
'option_title' => $option_data[$key2]['option_title']
];
} else if($question->q_type == 3) {
$question->options = [];
}
}
}
And it provided following result:
"question_data": [
{
"question_id": 2,
"title": "Describe about visit",
"q_type": 3,
"question_group_id": 1,
"options": []
},
{
"question_id": 3,
"title": "How to upload data",
"q_type": 2,
"question_group_id": 1,
"options": [
{
"question_id": 3,
"option_id": 4,
"option_title": "By any device"
},
{
"question_id": 3,
"option_id": 5,
"option_title": "By mobile"
},
{
"question_id": 3,
"option_id": 6,
"option_title": "By camera"
},
{
"question_id": 3,
"option_id": 7,
"option_title": "By Laptop"
},
{
"question_id": 3,
"option_id": 8,
"option_title": "No answer"
}
]
},
{
"question_id": 4,
"title": "How to add question?",
"q_type": 3,
"question_group_id": 1,
"options": []
},
{
"question_id": 5,
"title": "Multi selection is working?",
"q_type": 1,
"question_group_id": 1,
"options": [
{
"question_id": 5,
"option_id": 9,
"option_title": "Yes"
},
{
"question_id": 5,
"option_id": 10,
"option_title": "No"
},
{
"question_id": 5,
"option_id": 11,
"option_title": "Not Confirmed"
},
{
"question_id": 5,
"option_id": 12,
"option_title": "In progress"
},
{
"question_id": 5,
"option_id": 13,
"option_title": "Testing"
}
]
}
]

Related

Amadeus Flight Offers Price API issue

I want to create booking and generate PNR using amadeus API
I have follow step
serach with origin and destination and one way trip to get list of result using below API
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search/api-reference
Then verify selected offer price to get actual flight fare price.To verify selected offer price I have used below API
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-price/api-reference
But I am facing below issue to verify flight offer price and get actual flight fare.
I got list of flight offer price when search flght with "BLR" as from and "LAX" as to and one way trip using amadeus flight-offers-search API.
I have called another api "flight-offers-price" to verify flight offer price to get actual offer price.
I got response with actual flight fare price and show result when search filght with "BLR" as from and "LAX" as to and one way trip
i have use this API
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-price/api-reference
This is my request parameter
{
"data": {
"type": "flight-offers-pricing",
"flightOffers": [
{
"type": "flight-offer",
"id": "1",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"oneWay": false,
"lastTicketingDate": "2021-07-06",
"numberOfBookableSeats": 4,
"itineraries": [
{"duration": "PT30H58M",
"segments": [
{"departure": {"iataCode": "BLR","at": "2021-07-12T03:05:00"},
"arrival": {"iataCode": "FRA","terminal": "1","at": "2021-07-12T09:00:00"
},
"carrierCode": "AC",
"number": "9057",
"aircraft": {"code": "74H"},
"operating": {"carrierCode": "LH"},
"duration": "PT9H25M",
"id": "256",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {"iataCode": "FRA","terminal": "1","at": "2021-07-12T13:20:00"
},
"arrival": {"iataCode": "YVR","terminal": "M","at": "2021-07-12T14:15:00"
},
"carrierCode": "AC",
"number": "9101",
"aircraft": {"code": "744"},
"operating": {"carrierCode": "LH"},
"duration": "PT9H55M",
"id": "257",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {
"iataCode": "YVR",
"terminal": "M",
"at": "2021-07-12T18:35:00"
},
"arrival": {
"iataCode": "LAX",
"terminal": "6",
"at": "2021-07-12T21:33:00"
},
"carrierCode": "AC",
"number": "558",
"aircraft": {"code": "320"},
"operating": {"carrierCode": "AC"},
"duration": "PT2H58M",
"id": "258",
"numberOfStops": 0,
"blacklistedInEU": false
}
]
}
],
"price": {
"currency": "USD",
"total": "545.62",
"base": "201.00",
"fees": [
{"amount": "0.00","type": "SUPPLIER"},
{"amount": "0.00","type": "TICKETING"
}
],
"grandTotal": "545.62"
},
"pricingOptions": {
"fareType": ["PUBLISHED"],
"includedCheckedBagsOnly": true
},
"validatingAirlineCodes": ["AC"],
"travelerPricings": [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {"currency": "USD","total": "545.62","base": "201.00"},
"fareDetailsBySegment": [
{
"segmentId": "256",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"class": "K",
"includedCheckedBags": {
"quantity": 2
}
},
{
"segmentId": "257",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"class": "K",
"includedCheckedBags": {"quantity": 2}
},
{
"segmentId": "258",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"brandedFare": "STANDARD",
"class": "K",
"includedCheckedBags": {"quantity": 2}
}
]
}
]
}
]
}
}
This is my API Response
stdClass Object
(
[data] => stdClass Object (
[type] => flight-offers-pricing
[flightOffers] => Array (
[0] => stdClass Object (
[type] => flight-offer
[id] => 1
[source] => GDS
[instantTicketingRequired] =>
[nonHomogeneous] =>
[paymentCardRequired] =>
[lastTicketingDate] => 2021-07-06
[itineraries] => Array(
[0] => stdClass Object(
[segments] => Array(
[0] => stdClass Object (
[departure] => stdClass Object (
[iataCode] => BLR
[at] => 2021-07-12T03:05:00
)
[arrival] => stdClass Object (
[iataCode] => FRA
[terminal] => 1
[at] => 2021-07-12T09:00:00
)
[carrierCode] => AC
[number] => 9057
[aircraft] => stdClass Object (
[code] => 74H
)
[operating] => stdClass Object (
[carrierCode] => LH
)
[duration] => PT9H25M
[id] => 256
[numberOfStops] => 0
[co2Emissions] => Array(
[0] => stdClass Object(
[weight] => 353
[weightUnit] => KG
[cabin] => ECONOMY
)
)
)
[1] => stdClass Object (
[departure] => stdClass Object (
[iataCode] => FRA
[terminal] => 1
[at] => 2021-07-12T13:20:00
)
[arrival] => stdClass Object (
[iataCode] => YVR
[terminal] => M
[at] => 2021-07-12T14:15:00
)
[carrierCode] => AC
[number] => 9101
[aircraft] => stdClass Object
(
[code] => 744
)
[operating] => stdClass Object
(
[carrierCode] => LH
)
[duration] => PT9H55M
[id] => 257
[numberOfStops] => 0
[co2Emissions] => Array
(
[0] => stdClass Object
(
[weight] => 502
[weightUnit] => KG
[cabin] => ECONOMY
)
)
)
[2] => stdClass Object
(
[departure] => stdClass Object
(
[iataCode] => YVR
[terminal] => M
[at] => 2021-07-12T18:35:00
)
[arrival] => stdClass Object
(
[iataCode] => LAX
[terminal] => 6
[at] => 2021-07-12T21:33:00
)
[carrierCode] => AC
[number] => 558
[aircraft] => stdClass Object
(
[code] => 320
)
[operating] => stdClass Object
(
[carrierCode] => AC
)
[duration] => PT2H58M
[id] => 258
[numberOfStops] => 0
[co2Emissions] => Array
(
[0] => stdClass Object
(
[weight] => 171
[weightUnit] => KG
[cabin] => ECONOMY
)
)
)
)
)
)
[price] => stdClass Object
(
[currency] => USD
[total] => 545.62
[base] => 201.00
[fees] => Array
(
[0] => stdClass Object
(
[amount] => 0.00
[type] => SUPPLIER
)
[1] => stdClass Object
(
[amount] => 0.00
[type] => TICKETING
)
[2] => stdClass Object
(
[amount] => 0.00
[type] => FORM_OF_PAYMENT
)
)
[grandTotal] => 545.62
[billingCurrency] => USD
)
)
)
But when i serach flight with "CCU" as from and "Lax" as to and one way trip then i got list of flight fare price offer.
and choose specific price to booking then i called same API with same request parameter but value was different depending
on origin and destination to verify flight fare and get actual flight price,then API return response with error "INVALID DATA RECEIVED" code : 4926.
I have use these api
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-price/api-reference
This is my another api request parameter
{
"data": {
"type": "flight-offers-pricing",
"flightOffers": [
{
"type": "flight-offer",
"id": "1",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"oneWay": false,
"lastTicketingDate": "2021-07-06",
"numberOfBookableSeats": 3,
"itineraries": [
{
"duration": "PT30H38M",
"segments": [
{
"departure": {
"iataCode": "CCU",
"at": "2021-07-12T15:20:00"
},
"arrival": {
"iataCode": "DEL",
"terminal": "3",
"at": "2021-07-12T17:45:00"
},
"carrierCode": "UA",
"number": "7726",
"aircraft": {
"code": "320"
},
"operating": {
"carrierCode": "UK"
},
"duration": "PT2H25M",
"id": "141",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {
"iataCode": "DEL",
"terminal": "3",
"at": "2021-07-12T23:35:00"
},
"arrival": {
"iataCode": "EWR",
"terminal": "B",
"at": "2021-07-13T04:30:00"
},
"carrierCode": "UA",
"number": "83",
"aircraft": {
"code": "77W"
},
"operating": {
"carrierCode": "UA"
},
"duration": "PT14H25M",
"id": "142",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {
"iataCode": "EWR",
"terminal": "C",
"at": "2021-07-13T06:00:00"
},
"arrival": {
"iataCode": "LAX",
"terminal": "7",
"at": "2021-07-13T09:28:00"
},
"carrierCode": "UA",
"number": "2434",
"aircraft": {
"code": "752"
},
"operating": {
"carrierCode": "UA"
},
"duration": "PT6H28M",
"id": "143",
"numberOfStops": 0,
"blacklistedInEU": false
}
]
}
],
"price": {
"currency": "USD",
"total": "712.89",
"base": "336.00",
"fees": [
{
"amount": "0.00",
"type": "SUPPLIER"
},
{
"amount": "0.00",
"type": "TICKETING"
}
],
"grandTotal": "712.89"
},
"pricingOptions": {
"fareType": [
"PUBLISHED"
],
"includedCheckedBagsOnly": true
},
"validatingAirlineCodes": [
"UA"
],
"travelerPricings": [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "USD",
"total": "712.89",
"base": "336.00"
},
"fareDetailsBySegment": [
{
"segmentId": "141",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"class": "K",
"includedCheckedBags": {
"quantity": 1
}
},
{
"segmentId": "142",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"class": "K",
"includedCheckedBags": {
"quantity": 1
}
},
{
"segmentId": "143",
"cabin": "ECONOMY",
"fareBasis": "KRCZOAAW",
"brandedFare": "ECONOMY",
"class": "K",
"includedCheckedBags": {
"quantity": 1
}
}
]
}
]
}
]
}
}
This is the API response
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[code] => 4926
[title] => INVALID DATA RECEIVED
[detail] => No fare applicable
[status] => 400
)
)
)
That mean API return error using some specific origin and destination to get actual flight price for booking.
please guide me how to find out the issue and solve it
Thanks
This can happen when the flight is either sold out or there is no more fares available in the class you are targeting. It can even happen after calling Flight Offers Price if the last seat is booked before you complete the booking. This is normal behaviour and when performing many bookings in the test environment at once, please try to change the dates or the segments to avoid this.

how to remove the duplicates within an array of arrays based on the array key in php?

I have this multi dimensional array. At first glance it's duplicated because the id is the same for all three. but if you check the "notes" key, it's not. but I want to remove the duplicates from this array based form the "id" . how to do that? because array_unique does not work
[
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "Created",
"type_id": 25185,
"state_id": 0
},
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "Accepted",
"type_id": 25185,
"state_id": 0
},
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "In Progress",
"type_id": 25185,
"state_id": 0
}
];
Or shorter:
$result = array_values(array_column($yourArray, null, 'id')))
$result:
Array
(
[0] => stdClass Object
(
[id] => 69
[text] => hello text
[desc] => hello desc
[state] =>
[type] =>
[notes] => In Progress
[type_id] => 25185
[state_id] => 0
)
)
This will delete all but the last object with a matching id:
https://paiza.io/projects/mZ-e3zzmyFWq6XY-qjKLjg
<?php
$arr = [
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "Created",
"type_id" => 25185,
"state_id"=> 0
),
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "Accepted",
"type_id" => 25185,
"state_id" => 0
),
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "In Progress",
"type_id" => 25185,
"state_id" => 0
)
];
foreach($arr as $key1 => $obj1){
foreach($arr as $key2 => $obj2){
if($obj1->id === $obj2->id && $key1 !== $key2){
unset($arr[$key1]);
}
}
}
print_r($arr);
?>
Result:
Array
(
[2] => stdClass Object
(
[id] => 69
[text] => hello text
[desc] => hello desc
[state] =>
[type] =>
[notes] => In Progress
[type_id] => 25185
[state_id] => 0
)
)

data from json array in by php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am so close given everything I have read. Really I have been working on this and trying to understand this for a couple days. Endless google searches, and php manual reading of examples. I just don't think I have hit that AHA! moment where it clicks with me.
I want to get data from js file in anther server.
<?PHP
$token = "123";
//setup the request, you can also use CURLOPT_URL
$ch = curl_init('http://www.123.com/api/channels?limit=100000');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
$data = curl_exec($ch);
curl_close($ch);
//$xx = json_decode($data, true);
$someArray = json_decode($data, true);
//print_r($someArray);
foreach ($someArray as $key => $value) {
echo $key["id"] . ", " . $key["text"] . "<br>";
}
?>
** tried to parse a JSON file using PHP. But I am stuck now.
This is the content of my JSON file outbout**
{
"code": 100,
"message": "get successfully",
"all": [{
"id": 1,
"image": "http:\/\/www.live-plus.io\/cache\/d7\/23d79957adf9dc7d8e675f0cfa76a55d.png",
"text": "Bein Sport",
"order": 1,
"is_active": 1,
"created_at": "2017-10-22 00:03:08",
"updated_at": "2018-05-08 16:22:53",
"channels": [{
"id": 1,
"name": "bein News",
"image": "http:\/\/www.live-plus.io\/cache\/35\/7835dc385c571b09cbb8caabc07dcdb4.png",
"is_active": 1,
"is_feature": 0,
"order": 1,
"created_at": "2017-10-22 00:03:51",
"updated_at": "2018-01-18 21:22:45",
"pivot": {
"category_id": 1,
"channel_id": 1
},
"servers": [{
"id": 87,
"title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
"url": "redirect\/sport1\/BeinSportNews_source",
"type": 2,
"is_active": 1,
"channel_id": 1,
"server_type_id": 2,
"created_at": "2017-10-22 11:48:56",
"updated_at": "2018-08-17 12:54:26",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=IKcJuqTDez4txx5PLD2-eLPJx07T0UCyCgMJTWqavlg="
}, {
"id": 89,
"title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
"url": "redirect\/sport1\/BeinSportNews_360p",
"type": 2,
"is_active": 1,
"channel_id": 1,
"server_type_id": 2,
"created_at": "2017-10-22 11:50:23",
"updated_at": "2018-08-17 12:54:35",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=3mxu47vfYmQ5EwLoWNd2_Ni-j7hKWEKnc7BTgcVVgOM="
}, {
"id": 659,
"title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
"url": "redirect\/sport1\/BeinSportNews_240p",
"type": 2,
"is_active": 1,
"channel_id": 1,
"server_type_id": 2,
"created_at": "2018-03-30 14:49:50",
"updated_at": "2018-08-17 12:54:39",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HEIr-OaAmNDLE_gqvVPj8-uW6FcaG190ZLDzlNRM6mk="
}
]
}, {
"id": 2,
"name": "bein HD",
"image": "http:\/\/www.live-plus.io\/cache\/7d\/6e7dd27da3b4a9ed33dca15307ba879f.png",
"is_active": 1,
"is_feature": 0,
"order": 2,
"created_at": "2017-10-22 00:04:12",
"updated_at": "2018-01-18 21:24:14",
"pivot": {
"category_id": 1,
"channel_id": 2
},
"servers": [{
"id": 90,
"title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
"url": "redirect\/sport1\/beINSPORTSHD_source",
"type": 2,
"is_active": 1,
"channel_id": 2,
"server_type_id": 2,
"created_at": "2017-10-22 11:50:57",
"updated_at": "2018-08-17 12:54:43",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=zziSNmcRfuPO8du2h-u4meRkuLV40G3SBVcGTqVmYUc="
}, {
"id": 92,
"title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
"url": "redirect\/sport1\/beINSPORTSHD_360p",
"type": 2,
"is_active": 1,
"channel_id": 2,
"server_type_id": 2,
"created_at": "2017-10-22 11:52:03",
"updated_at": "2018-08-17 12:54:47",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=6JxLzcmNV1wdQN8ltaHaBEBfJz6Q7xwNz2UklshsLuw="
}, {
"id": 661,
"title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
"url": "redirect\/sport1\/beINSPORTSHD_240p",
"type": 2,
"is_active": 1,
"channel_id": 2,
"server_type_id": 2,
"created_at": "2018-03-30 14:54:13",
"updated_at": "2018-08-17 12:54:52",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HMIy3NwWgJzMCxp8TCAO55AMhM_QT0DxChNU6nAUyIo="
}
]
}, {
"id": 3,
"name": "Bein Sports 1",
"image": "http:\/\/www.live-plus.io\/cache\/ff\/59ffb0c1f6fc5f230f7afcc750da6f12.png",
"is_active": 1,
"is_feature": 0,
"order": 3,
"created_at": "2017-10-22 00:04:34",
"updated_at": "2018-01-17 17:18:13",
"pivot": {
"category_id": 1,
"channel_id": 3
},
"servers": [{
"id": 1,
"title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720)",
"url": "redirect\/Beinx1\/bx1_source",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:30:21",
"updated_at": "2018-08-17 18:08:16",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=85LyxJDxf9i0bU6kioFowv2I-Qon_0xPNb_M9G_kBqM="
}, {
"id": 2,
"title": "\u062c\u0648\u062f\u0629 \u0645\u062a\u0648\u0633\u0637\u0629 (480)",
"url": "redirect\/Beinx1\/bx1_480p",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:33:39",
"updated_at": "2018-08-17 18:08:20",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_480p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=0vjltj0WknuGZ2ZH5yVdGASflpXN9WtYtZGi-PQdJ-A="
}, {
"id": 3,
"title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
"url": "redirect\/Beinx1\/bx1_360p",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:34:14",
"updated_at": "2018-08-17 18:08:24",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=S8vjztw7O8vZJfoDqmMWQ8Kd5OXgh624Fz3H1mDOdn0="
}, {
"id": 4,
"title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 (240)",
"url": "redirect\/Beinx1\/bx1_240p",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:34:33",
"updated_at": "2018-08-17 18:08:28",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=kcIoL7nR7fGRfJ_HoA747nbjiAjphEncaj1JpIJ8_dc="
}, {
"id": 6,
"title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 - \u0645\u062e\u0635\u0635\u0647 \u0644\u0644\u0628\u0627\u0642\u0629",
"url": "redirect\/Beinx1\/bx1_160p",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:38:25",
"updated_at": "2018-08-17 18:08:32",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_160p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=nS6iGwp-kLIOMsIWygOnuamfFq13KKjJhJgJ0i2lAos="
}, {
"id": 7,
"title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720) \u0645\u0635\u062f\u0631 2",
"url": "redirect\/Beinx3\/x1_source",
"type": 2,
"is_active": 1,
"channel_id": 3,
"server_type_id": 2,
"created_at": "2017-10-22 00:38:47",
"updated_at": "2018-08-17 18:14:34",
"secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx3\/x1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=1MuhTMmCh8l8EBcfMrhpRmLt8XbeVCIlwF5zDk6sbfg="
I need to get secure_url by name item
Theoretically, you should need three criterias, in order to find a specific secure_url:
The id of an element in the $decodedData['all'] array ($searchedAllId);
The id of a channel in the channels list of the above array element ($searchedChannelId);
The id of a server in the servers list of the above channel element ($searchedServerId).
If you have other requirement(s) or perspective, then tell us in (more) detail, so that we can give you the answer suited to the new situation(s).
I think you can easily follow the steps I used in the code. I didn't apply any validation on the array elements, in order to keep it simple.
//...
$data = curl_exec($ch);
curl_close($ch);
$decodedData = json_decode($data, true);
//=========================================================
// This line is just for testing: display the decoded data.
//echo '<pre>' . print_r($decodedData, TRUE) . '</pre>';
//=========================================================
//
// Criterias to search for in the decoded data.
$searchedAllId = 1;
$searchedChannelId = 1;
$searchedServerId = 659;
// Holds the found secure url.
$foundSecureUrl = '';
// Search for the secure url based on the above criterias.
foreach ($decodedData['all'] as $allKey => $allItem) {
if ($allItem['id'] === $searchedAllId) {
foreach ($allItem['channels'] as $channelKey => $channelItem) {
if ($channelItem['id'] === $searchedChannelId) {
foreach ($channelItem['servers'] as $serverKey => $serverItem) {
if ($serverItem['id'] === $searchedServerId) {
$foundSecureUrl = $serverItem['secure_url'];
/*
* Secure url is now found.
* So break the further execution of all THREE foreach loops.
*/
break(3);
}
}
}
}
}
}
// Display the found secure url.
echo 'Found secure url:<br/><br/>' . $foundSecureUrl;
The $decodedData array looks like this, after I shorted it and changed the sensible data in it:
Array
(
[code] => 100
[message] => get successfully
[all] => Array
(
[0] => Array
(
[id] => 1
[image] => some-image.png
[text] => Bein Sport
[order] => 1
[is_active] => 1
[created_at] => 2017-10-22 00:03:08
[updated_at] => 2018-05-08 16:22:53
[channels] => Array
(
[0] => Array
(
[id] => 1
[name] => bein News
[image] => some-image.png
[is_active] => 1
[is_feature] => 0
[order] => 1
[created_at] => 2017-10-22 00:03:51
[updated_at] => 2018-01-18 21:22:45
[pivot] => Array
(
[category_id] => 1
[channel_id] => 1
)
[servers] => Array
(
[0] => Array
(
[id] => 87
[title] => جوده عاليه
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 1
[server_type_id] => 2
[created_at] => 2017-10-22 11:48:56
[updated_at] => 2018-08-17 12:54:26
[secure_url] => some-secure-url
)
[1] => Array
(
[id] => 89
[title] => جوده متوسطة (360)
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 1
[server_type_id] => 2
[created_at] => 2017-10-22 11:50:23
[updated_at] => 2018-08-17 12:54:35
[secure_url] => some-secure-url
)
...
)
)
[1] => Array
(
[id] => 2
[name] => bein HD
[image] => some-image.png
[is_active] => 1
[is_feature] => 0
[order] => 2
[created_at] => 2017-10-22 00:04:12
[updated_at] => 2018-01-18 21:24:14
[pivot] => Array
(
[category_id] => 1
[channel_id] => 2
)
[servers] => Array
(
[0] => Array
(
[id] => 90
[title] => جوده عاليه
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 2
[server_type_id] => 2
[created_at] => 2017-10-22 11:50:57
[updated_at] => 2018-08-17 12:54:43
[secure_url] => some-secure-url
)
[1] => Array
(
[id] => 92
[title] => جوده متوسطة (360)
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 2
[server_type_id] => 2
[created_at] => 2017-10-22 11:52:03
[updated_at] => 2018-08-17 12:54:47
[secure_url] => some-secure-url
)
...
)
)
...
)
)
[1] => Array
(
[id] => 2
[image] => some-image.png
[text] => bein
[order] => 2
[is_active] => 1
[created_at] => 2017-10-22 12:53:17
[updated_at] => 2018-05-08 16:22:58
[channels] => Array
(
[0] => Array
(
[id] => 536
[name] => BBC EARTH
[image] => some-image.png
[is_active] => 1
[is_feature] => 0
[order] => 0
[created_at] => 2018-08-17 15:53:06
[updated_at] => 2018-08-17 15:53:06
[pivot] => Array
(
[category_id] => 2
[channel_id] => 536
)
[servers] => Array
(
[0] => Array
(
[id] => 1136
[title] => جودة عالية
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 536
[server_type_id] => 2
[created_at] => 2018-08-17 16:01:26
[updated_at] => 2018-08-17 16:01:26
[secure_url] => some-secure-url
)
[1] => Array
(
[id] => 1137
[title] => جوده متوسطة
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 536
[server_type_id] => 2
[created_at] => 2018-08-17 16:01:58
[updated_at] => 2018-08-17 16:01:58
[secure_url] => some-secure-url
)
...
)
)
[1] => Array
(
[id] => 535
[name] => BeIN Dlife
[image] => some-image.png
[is_active] => 1
[is_feature] => 0
[order] => 0
[created_at] => 2018-08-17 15:52:15
[updated_at] => 2018-08-17 15:52:15
[pivot] => Array
(
[category_id] => 2
[channel_id] => 535
)
[servers] => Array
(
[0] => Array
(
[id] => 1133
[title] => جودة عالية
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 535
[server_type_id] => 2
[created_at] => 2018-08-17 16:00:13
[updated_at] => 2018-08-17 16:00:13
[secure_url] => some-secure-url
)
[1] => Array
(
[id] => 1134
[title] => جودة متوسطة
[url] => some-url
[type] => 2
[is_active] => 1
[channel_id] => 535
[server_type_id] => 2
[created_at] => 2018-08-17 16:00:35
[updated_at] => 2018-08-17 16:00:35
[secure_url] => some-secure-url
)
...
)
)
...
)
)
...
)
)

Extracting hierachy structure into json

i am having trouble getting my mysql table into json.
I have a table that show the hierachy of employees like that:
The table is a employee table with
id, firstname, lastname, parentid
So now I want to query through the data to extract it into a multi level json object to simulate show the employees with their supervisors.
I hope somebody can help me with it.
Assuming you've already taken data from the database, below solution will get you what you need.
The way to go about it is via recursion.
php sandbox online
<?php
function buildTree(array &$elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parentid'] == $parentId) {
// recursion:
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['id']] = $element;
unset($elements[$element['id']]);
}
}
return $branch;
}
$rows = [
['id' => 1, 'firstname' => 'john1', 'lastname' => 'doe', 'parentid' => 0],
['id' => 2, 'firstname' => 'john2', 'lastname' => 'doe', 'parentid' => 1],
['id' => 3, 'firstname' => 'john3', 'lastname' => 'doe', 'parentid' => 1],
['id' => 4, 'firstname' => 'john4', 'lastname' => 'doe', 'parentid' => 0],
['id' => 5, 'firstname' => 'john5', 'lastname' => 'doe', 'parentid' => 3],
];
print_r(buildTree($rows));
and the resulting array:
Array
(
[1] => Array
(
[id] => 1
[firstname] => john1
[lastname] => doe
[parentid] => 0
[children] => Array
(
[2] => Array
(
[id] => 2
[firstname] => john2
[lastname] => doe
[parentid] => 1
)
[3] => Array
(
[id] => 3
[firstname] => john3
[lastname] => doe
[parentid] => 1
[children] => Array
(
[5] => Array
(
[id] => 5
[firstname] => john5
[lastname] => doe
[parentid] => 3
)
)
)
)
)
[4] => Array
(
[id] => 4
[firstname] => john4
[lastname] => doe
[parentid] => 0
)
)
After you json_encode the result, you will get:
{
"1": {
"id": 1,
"firstname": "john1",
"lastname": "doe",
"parentid": 0,
"children": {
"2": {
"id": 2,
"firstname": "john2",
"lastname": "doe",
"parentid": 1
},
"3": {
"id": 3,
"firstname": "john3",
"lastname": "doe",
"parentid": 1,
"children": {
"5": {
"id": 5,
"firstname": "john5",
"lastname": "doe",
"parentid": 3
}
}
}
}
},
"4": {
"id": 4,
"firstname": "john4",
"lastname": "doe",
"parentid": 0
}
}

How to convert JSON string in Array. Array within Array stuck me. How to handle with it?

I have a JSON String. I want it to be filled in Array. Here is Array which i receive.
My JSON Response is:
{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}
I want to store all this data in Array and from that in DataBase.
If you want this json string in an array than you can use
json_decode($string,true);
Note that, second param of json_decode will return the array if you need result in object form than remove the second param "true".
Your Code:
$string = '{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}';
$array = json_decode($string,true);
echo "<pre>";
print_r($array);
Result:
Array
(
[model] => SyncData
[unique_id] => c12fb356f90d032b
[key] => sdjvnsdivbsnd
[sync_data] => Array
(
[array_a] => Array
(
[0] => Array
(
[a_fav] => true
[a_id] => 1
)
[1] => Array
(
[a_fav] => false
[a_id] => 2
)
)
[array_b] => Array
(
[0] => Array
(
[b_fav] => false
[b_id] => 8
)
)
[c_array] => Array
(
[0] => Array
(
[c_fav] => false
[c_id] => 15996
)
)
[patient_list_array] => Array
(
[0] => Array
(
[unique_id] => sdvsdvsdvdsdv
[p_status] => 1
[p_id] => 1454943805215
[p_note] => 2
[p_code] => 8
[p_timestamp] => 1454943805216
[p_name] => ABC XYZ
[p_room_no] => 5
)
)
[array_d] => Array
(
[0] => Array
(
[d_assigned_id] => 30
[d_fav] => true
)
)
)
)
Simple code to use to visualise your JSON String
<?php
$string = '{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}';
$obj = json_decode($string);
print_r($obj);
Which will output :
stdClass Object
(
[model] => SyncData
[unique_id] => c12fb356f90d032b
[key] => sdjvnsdivbsnd
[sync_data] => stdClass Object
(
[array_a] => Array
(
[0] => stdClass Object
(
[a_fav] => true
[a_id] => 1
)
[1] => stdClass Object
(
[a_fav] => false
[a_id] => 2
)
)
[array_b] => Array
(
[0] => stdClass Object
(
[b_fav] => false
[b_id] => 8
)
)
[c_array] => Array
(
[0] => stdClass Object
(
[c_fav] => false
[c_id] => 15996
)
)
[patient_list_array] => Array
(
[0] => stdClass Object
(
[unique_id] => sdvsdvsdvdsdv
[p_status] => 1
[p_id] => 1454943805215
[p_note] => 2
[p_code] => 8
[p_timestamp] => 1454943805216
[p_name] => ABC XYZ
[p_room_no] => 5
)
)
[array_d] => Array
(
[0] => stdClass Object
(
[d_assigned_id] => 30
[d_fav] => true
)
)
)
)

Categories