Does anyone know how to get the number of occurences of "photo" and "status"? I tried to output it as a string and perform a substr_count but no luck.
Array ( [0] => [1] => photo )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => status )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => status )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => status )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => photo )
Array ( [0] => [1] => status )
Array ( [0] => [1] => status )
Array ( [0] => [1] => status )
Array ( [0] => [1] => link )
Array ( [0] => [1] => status )
The original JSON code from the Facebook Graph API is:
{
"data": [
{
"id": "123",
"from": {
"name": "Some Name",
"category": "Food/beverages",
"id": "123"
},
"picture": "a pic",
"link": "a link",
"name": "bla bla ",
"caption": "9 new photos",
"icon": "a icon",
"actions": [
{
"name": "Comment",
"link": "a link"
},
{
"name": "Like",
"link": "a link"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE"
},
"type": "photo",
"object_id": "232342",
"created_time": "2012-03-01T14:58:53+0000",
"updated_time": "2012-03-02T14:12:50+0000",
"likes": {
"data": [
{
"name": "a name",
"id": "1234423"
}
],
"count": 58
},
"comments": {
"data": [
{
"id": "234234",
"from": {
"name": "a name",
"id": "23423234"
},
"message": "message bla bla",
"created_time": "2012-03-02T13:42:48+0000",
"likes": 2
},
{
"id": "234234234",
"from": {
"name": " a name",
"category": "Food/beverages",
"id": "123"
},
"message": "asfd",
"created_time": "2012-03-02T14:12:50+0000"
}
],
"count": 15
},
"is_published": true
},
{
"id": "123123",
"from": {
"name": "name",
"category": "Food/beverages",
"id": "123"
},
"message": "sadfasfd",
"actions": [
{
"name": "Comment",
"link": "a link"
},
{
"name": "Like",
"link": "a link"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE"
},
"type": "status",
"created_time": "2012-03-01T09:49:40+0000",
"updated_time": "2012-03-01T17:00:53+0000",
"likes": {
"data": [
{
"name": "name",
"id": "1000"
}
],
"count": 17
},
"comments": {
"data": [
{
"id": "1404",
"from": {
"name": "name",
"id": "1000"
},
"message": "bla bla",
"created_time": "2012-03-01T11:41:53+0000",
"likes": 2
},
{
"id": "1404781",
"from": {
"name": "name",
"id": "142"
},
"message": "message adsafasd",
"created_time": "2012-03-01T17:00:53+0000"
}
],
"count": 9
},
"is_published": true
},
I perform a json_decode with the JSON output and then loop through like that
foreach ($page_posts_overview->data as $page_posts_overview_output){
$type = " ".$page_posts_overview_output->type;
$type = explode(" ", $type);
$type = array($type);
print_r ($type);// this is what the output posted above
}
I think you would need to loop through the array manually and keep count of the number of times each word shows up:
function count_words($fb_array) {
$keywords = array();
foreach($fb_array as $element) {
$word = $element[1];
if (!array_key_exists($keywords, $word)) {
$keywords[$word] = 0;
}
$keywords[$word]++;
}
return $keywords;
}
print_r($keywords);
Assuming you have an array of keywords in $fb_array, $keywords should now contain a count for each word.
$a = array(...); // your Facebook data
$b = array();
foreach ($a as $e) {
$b[] = $e[1];
}
$c = array_count_values($b);
var_dump($c)
http://www.php.net/manual/en/function.array-count-values.php
Related
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.
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
)
)
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"
}
]
}
]
I have following code to get json string from YouTube:
$dataSnippet = file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet&id=jZT82WmOTkw&key=[api-key]');
This is the content (not all of it):
string(3252) "{ "kind": "youtube#videoListResponse", "etag":
"\"XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0\"",
"pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items":
[ { "kind": "youtube#video", "etag":
"\"XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8\"",
"id": "Mpxr7Rc0ycQ", "snippet": {
"publishedAt": "2018-07-18T11:29:51.000Z",
"channelId": "UC8ezvxw6dD3dF5Xu9Aw7icA",
"title": "5 Weird Things Caught on Security Cameras & CCTV #2",
"description": "description",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg",
"width": 640,
"height": 480
}, ......
But when i put a json_decode (assoc: true) over it I get this:
array(4) { ["kind"]=> string(25) "youtube#videoListResponse"
["etag"]=> string(57)
""XI7nbFXulYBIpL0ayR_gDh3eu1k/PPyttIkZuikajCWylsl_R32g_pE""
["pageInfo"]=> array(2) {
["totalResults"]=>
int(1)
["resultsPerPage"]=>
int(1) } ["items"]=> array(1) {
[0]=>
string(8) "Array(4)" } }
especially this part is strange:
["items"]=> array(1) {
[0]=>
string(8) "Array(4)" } }
How can I get a array with objects? Tried many things without a result.
The issue is you're using json_decode($val, assoc: true)
From the php documentation:
When TRUE, returned objects will be converted into associative arrays.
So, just don't use true, and it should treat things the way you want.
<?php
$json = '{
"kind": "youtube#videoListResponse",
"etag": "XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8",
"id": "Mpxr7Rc0ycQ",
"snippet": {
"publishedAt": "2018-07-18T11:29:51.000Z",
"channelId": "UC8ezvxw6dD3dF5Xu9Aw7icA",
"title": "5 Weird Things Caught on Security Cameras & CCTV #2",
"description": "description",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg",
"width": 640,
"height": 480
}
}
}
}
]
}';
$val = json_decode($json);
print_r($val);
Converts to:
stdClass Object
(
[kind] => youtube#videoListResponse
[etag] => XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0
[pageInfo] => stdClass Object
(
[totalResults] => 1
[resultsPerPage] => 1
)
[items] => Array
(
[0] => stdClass Object
(
[kind] => youtube#video
[etag] => XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8
[id] => Mpxr7Rc0ycQ
[snippet] => stdClass Object
(
[publishedAt] => 2018-07-18T11:29:51.000Z
[channelId] => UC8ezvxw6dD3dF5Xu9Aw7icA
[title] => 5 Weird Things Caught on Security Cameras & CCTV #2
[description] => description
[thumbnails] => stdClass Object
(
[default] => stdClass Object
(
[url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg
[width] => 120
[height] => 90
)
[medium] => stdClass Object
(
[url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => stdClass Object
(
[url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg
[width] => 480
[height] => 360
)
[standard] => stdClass Object
(
[url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg
[width] => 640
[height] => 480
)
)
)
)
)
)
stdClass being PHP's internal equivelant of a generic object.
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
)
)
)
)