normal Array to nested Json Conversion PHP - php

I have an array of hospital which doctors are visiting at different time of weekdays
eg:
Array
(
[0] => stdClass Object
(
[id] => 40
[doctorid] => 58
[hospitalid] => 1
[weekday] => 2
[starttime] => 09:30
[stime] => PM
[endtime] => 11:00
[etime] => PM
[randomkey] =>
)
[1] => stdClass Object
(
[id] => 41
[doctorid] => 58
[hospitalid] => 1
[weekday] => 2
[starttime] => 02:01
[stime] => PM
[endtime] => 04:02
[etime] => PM
[randomkey] =>
)
[2] => stdClass Object
(
[id] => 39
[doctorid] => 58
[hospitalid] => 1
[weekday] => 3
[starttime] => 09:30
[stime] => AM
[endtime] => 11:00
[etime] => AM
[randomkey] =>
)
[3] => stdClass Object
(
[id] => 38
[doctorid] => 58
[hospitalid] => 1
[weekday] => 3
[starttime] => 06:30
[stime] => PM
[endtime] => 09:30
[etime] => PM
[randomkey] =>
)
[4] => stdClass Object
(
[id] => 7
[doctorid] => 58
[hospitalid] => 2
[weekday] => 2
[starttime] => 09:30
[stime] => AM
[endtime] => 05:00
[etime] => PM
[randomkey] => rKkEU6cTWWN4ZxCw
)
[5] => stdClass Object
(
[id] => 8
[doctorid] => 58
[hospitalid] => 3
[weekday] => 2
[starttime] => 09:30
[stime] => AM
[endtime] => 05:00
[etime] => PM
[randomkey] => rKkEU6cTWWN4ZxCw
)
[6] => stdClass Object
(
[id] => 9
[doctorid] => 58
[hospitalid] => 23
[weekday] => 2
[starttime] => 09:30
[stime] => AM
[endtime] => 06:00
[etime] => PM
[randomkey] => rKkEU6cTWWN4ZxCw
)
)
I want the array in the following JSON tree format first order by hospital, days then time Eg:
{
"Hospital": [
{
"doctorid": "58",
"hospitalid": "1",
"day": [
{
"weekday": 2,
"time": [
{
"starttime": "9:30",
"stime": "AM",
"endtime": "11:00",
"etime": "PM"
},
{
"starttime": "2:01",
"stime": "PM",
"endtime": "04:02",
"etime": "PM"
}
]
},
{
"weekday": 3,
"time": [
{
"starttime": "9:30",
"stime": "AM",
"endtime": "11:00",
"etime": "PM"
},
{
"starttime": "6:30",
"stime": "PM",
"endtime": "09:30",
"etime": "PM"
}
]
}
]
},
{
"doctorid": "58",
"hospitalid": "2",
"day": [
{
"weekday": 2,
"time": [
{
"starttime": "9:30",
"stime": "AM",
"endtime": "05:00",
"etime": "PM"
}
]
}
]
},
{
"doctorid": "58",
"hospitalid": "3",
"day": [
{
"weekday": 2,
"time": [
{
"starttime": "9:30",
"stime": "AM",
"endtime": "05:00",
"etime": "PM"
}
]
}
]
},
{
"doctorid": "58",
"hospitalid": "23",
"day": [
{
"weekday": 3,
"time": [
{
"starttime": "9:30",
"stime": "AM",
"endtime": "06:00",
"etime": "PM"
}
]
}
]
}
]
}
I have tried to breakdown using foreach loop but can't solve it after hospital level. A generic functions will be helpful.

You need to go through your original array and create the final array, sorted as you want. Then calling json_encode($finalArray, true) will give you the json representation.
In your example, you can do something like this:
$sortedHospitals = [];
foreach ($hospitals as $hospital) {
$hopitalId = $hospital['id'];
$weekday = $hospital['weekday'];
$startTime = $hospital['startTime'];
$endTime = $hospital['endTime'];
if (array_key_exists($hospitalId, $sortedHospitals)) {
$currentHospital = $sortedHospitals[$hospitalId];
// add time to current hospital
}
else {
// create correct structure for the hospital
$currentHospital = [
'id' => $hospitalId,
'doctorId' => $hospital['doctorid'],
...
];
$sortedHospitals[$hospitalId] = $currentHospital;
}
}
return json_encode($sortedHospitals, true);

Related

i need help for empty array in array_column

I tried in many ways, but none is my specific problem, I tried to use what I know, but to no avail. I'm trying to use file_get_contents to get the json from 1 file.json on my server, and calculate a specific array, but after the "array_column" it returns an empty array.
<?php
$json_url = "http://myserver/file.json";
$json = file_get_contents($json_url);
$data = json_decode($json, true);
$price = array_column($data, 'valor');
print_r($price);
//output = array() | empyty array
the $json return the correct json,
in the $data return the corret decoded json,
but in $price, return empty array()
the $json content of my json file in my server is similar to it:
{
"1228421731": [
{
"valor": 10,
"datelimite": 1644024834,
"date": 1643420034,
"codigo": "ALYCGKU76S"
}
],
"1111925985": [
{
"valor": 25,
"datelimite": 1644066350,
"date": 1643461550,
"codigo": "HK8NQROGBW"
}
],
"1413051871": [
{
"valor": 50,
"datelimite": 1644084293,
"date": 1643479493,
"codigo": "4NGJ523J6H"
}
],
"1209626299": [
{
"valor": 10,
"datelimite": 1644091732,
"date": 1643486932,
"codigo": "W7LRGY3FHZ"
}
],
"1803561706": [
{
"valor": 10,
"datelimite": 1644257719,
"date": 1643652919,
"codigo": "GE8IDPZN9H"
},
{
"valor": 19865091226,
"datelimite": 1644288984,
"date": 1643684184,
"codigo": "F5Q9TPE43SWAFX"
}
],
"1710879952": [
{
"valor": 19865091226,
"datelimite": 1644274668,
"date": 1643669868,
"codigo": "P5M4E6RP1ZPEUR"
}
],
"1907375762": [
{
"valor": 10,
"datelimite": 1644377257,
"date": 1643772457,
"codigo": "E8OKPVCYVWRHUI"
}
],
"1863764959": [
{
"valor": 25,
"datelimite": 1644427955,
"date": 1643823155,
"codigo": "L78EZKJZJ93UYW"
}
],
"1831713303": [
{
"valor": 10,
"datelimite": 1644442109,
"date": 1643837309,
"codigo": "3J84VTS5FRS6OI"
}
],
"5193759120": [
{
"valor": 10,
"datelimite": 1644453308,
"date": 1643848508,
"codigo": "88942HFHZ5JJ56"
}
],
"1785872541": [
{
"valor": 10,
"datelimite": 1644504518,
"date": 1643899718,
"codigo": "LBML4O31RLC7DW"
}
],
"1666986497": [
{
"valor": 1,
"datelimite": 1644667829,
"date": 1644063029,
"codigo": "HXHPK6XLNBD89E"
}
]
}
how can i use array_column for this json type? for the array 'valor'?
$data is in this format
Array
(
[1228421731] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644024834
[date] => 1643420034
[codigo] => ALYCGKU76S
)
)
[1111925985] => Array
(
[0] => Array
(
[valor] => 25
[datelimite] => 1644066350
[date] => 1643461550
[codigo] => HK8NQROGBW
)
)
[1413051871] => Array
(
[0] => Array
(
[valor] => 50
[datelimite] => 1644084293
[date] => 1643479493
[codigo] => 4NGJ523J6H
)
)
[1209626299] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644091732
[date] => 1643486932
[codigo] => W7LRGY3FHZ
)
)
[1803561706] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644257719
[date] => 1643652919
[codigo] => GE8IDPZN9H
)
[1] => Array
(
[valor] => 19865091226
[datelimite] => 1644288984
[date] => 1643684184
[codigo] => F5Q9TPE43SWAFX
)
)
[1710879952] => Array
(
[0] => Array
(
[valor] => 19865091226
[datelimite] => 1644274668
[date] => 1643669868
[codigo] => P5M4E6RP1ZPEUR
)
)
[1907375762] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644377257
[date] => 1643772457
[codigo] => E8OKPVCYVWRHUI
)
)
[1863764959] => Array
(
[0] => Array
(
[valor] => 25
[datelimite] => 1644427955
[date] => 1643823155
[codigo] => L78EZKJZJ93UYW
)
)
[1831713303] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644442109
[date] => 1643837309
[codigo] => 3J84VTS5FRS6OI
)
)
[5193759120] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644453308
[date] => 1643848508
[codigo] => 88942HFHZ5JJ56
)
)
[1785872541] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644504518
[date] => 1643899718
[codigo] => LBML4O31RLC7DW
)
)
[1666986497] => Array
(
[0] => Array
(
[valor] => 1
[datelimite] => 1644667829
[date] => 1644063029
[codigo] => HXHPK6XLNBD89E
)
)
)
so I think you can try to get the first array element in each array and then operate on those arrays, like this
$price = array_column(array_column($data, 0),'valor');
This will give the results
You can try this
array_column(array_merge(...(json_decode($json,true))),"valor")
This will return an array with all the values of valor
If you want an array of all valor values:
$flatArray = array_reduce(
json_decode($json, true),
fn($carry, $item) => $carry = array_merge($carry, array_column($item, 'valor')),
[]
);
If you want an array grouped by valors belonging together:
$groupedArray = array_reduce(
json_decode($json, true),
fn($carry, $item) => $carry = array_merge($carry, [ array_column($item, 'valor') ]),
[])
;

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.

Google Calendar API marking new events as "cancelled"

I am trying to create events in my calendar via the calendar API.
Below is the object I am pushing to the API:
Google_Event Object
(
[__creatorType:protected] => Google_EventCreator
[__creatorDataType:protected] =>
[creator] => Google_EventCreator Object
(
[self] => 1
[displayName] =>
[email] => gcalevent#golden-hook-134443.iam.gserviceaccount.com
[id] =>
)
[__organizerType:protected] => Google_EventOrganizer
[__organizerDataType:protected] =>
[organizer] => Google_EventOrganizer Object
(
[self] => 1
[displayName] =>
[email] => gcalevent#golden-hook-134443.iam.gserviceaccount.com
[id] =>
)
[summary] => Hair: Elliot Test
[id] => 421a880v7dqb97p5voqsp198ac
[__attendeesType:protected] => Google_EventAttendee
[__attendeesDataType:protected] => array
[attendees] => Array
(
[0] => Google_EventAttendee Object
(
[comment] =>
[displayName] => Elliot Test
[responseStatus] => accepted
[self] =>
[id] =>
[additionalGuests] =>
[resource] =>
[organizer] =>
[optional] =>
[email] => hello#elliot.co.uk
)
)
[htmlLink] => https://www.google.com/calendar/event?eid=NDIxYTg4MHY3ZHFiOTdwNXZvcXNwMTk4YWMgZ2NhbGV2ZW50QGdvbGRlbi1ob29rLTE2NDQyMS5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbQ
[recurrence] =>
[__startType:protected] => Google_EventDateTime
[__startDataType:protected] =>
[start] => Google_EventDateTime Object
(
[date] =>
[timeZone] =>
[dateTime] => 2019-09-10T15:00:00Z
)
[etag] => "3136239546092000"
[location] =>
[recurringEventId] =>
[__gadgetType:protected] => Google_EventGadget
[__gadgetDataType:protected] =>
[gadget] =>
[status] => confirmed
[updated] => 2019-09-10T12:49:33.046Z
[description] => Price: £10.00
Services: Testing
[iCalUID] => 421a880v7dqb97p5voqsp198ac#google.com
[__extendedPropertiesType:protected] => Google_EventExtendedProperties
[__extendedPropertiesDataType:protected] =>
[extendedProperties] =>
[endTimeUnspecified] =>
[sequence] => 0
[visibility] =>
[guestsCanModify] =>
[__endType:protected] => Google_EventDateTime
[__endDataType:protected] =>
[end] => Google_EventDateTime Object
(
[date] =>
[timeZone] =>
[dateTime] => 2019-09-10T16:00:00Z
)
[attendeesOmitted] =>
[kind] => calendar#event
[locked] =>
[created] => 2019-09-10T12:49:33.000Z
[colorId] =>
[anyoneCanAddSelf] =>
[__remindersType:protected] => Google_EventReminders
[__remindersDataType:protected] =>
[reminders] => Google_EventReminders Object
(
[__overridesType:protected] => Google_EventReminder
[__overridesDataType:protected] => array
[overrides] =>
[useDefault] => 1
)
[guestsCanSeeOtherGuests] =>
[__originalStartTimeType:protected] => Google_EventDateTime
[__originalStartTimeDataType:protected] =>
[originalStartTime] =>
[guestsCanInviteOthers] =>
[transparency] =>
[privateCopy] =>
)
Which appears to be work fine, however when I go to retrieve that event using the ID: 421a880v7dqb97p5voqsp198ac it shows all the above details but the status is marked as "cancelled" and therefore it isn't showing in my calendar:
{
"kind": "calendar#event",
"etag": "\"3136239559346000\"",
"id": "421a880v7dqb97p5voqsp198ac",
"status": "cancelled",
"htmlLink": "https://www.google.com/calendar/event?eid=NDIxYTg4MHY3ZHFiOTdwNXZvcXNwMTk4YWMgaGVsbG9AZWxsaW90cmVldmUuY28udWs",
"created": "2019-09-10T12:49:33.000Z",
"updated": "2019-09-10T12:49:39.673Z",
"summary": "Hair: Elliot Test",
"description": "Price: £10.00\nSerivices: Testing",
"creator": {
"email": "gcalevent#golden-hook-134443.iam.gserviceaccount.com"
},
"organizer": {
"email": "gcalevent#golden-hook-134443.iam.gserviceaccount.com"
},
"start": {
"dateTime": "2019-09-10T16:00:00+01:00"
},
"end": {
"dateTime": "2019-09-10T17:00:00+01:00"
},
"iCalUID": "421a880v7dqb97p5voqsp198ac#google.com",
"sequence": 0,
"attendees": [
{
"email": "hello#elliot.co.uk",
"displayName": "Elliot Test",
"self": true,
"responseStatus": "accepted"
}
],
"reminders": {
"useDefault": true
}
}
This has previously been working fine for months and is now no longer working without any changes from my side.
Can anyone provide any insight into why this might be happening?
Thanks

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

Converting array to a tree-like JSON structure

Here's the array which I m trying to convert to a JSON Object:
Array
(
[0] => 102
[id] => 102
[1] =>
[attribution] =>
[2] => image
[type] => image
[3] =>
[latitude] =>
[4] =>
[longitude] =>
[5] => 81
[comments] => 81
[6] => Nashville
[filter] => Nashville
[7] => 1340803981
[created_time] => 1340803981
[8] => http://instagr.am/p/MYQ4IqTVHm/
[link] => http://instagr.am/p/MYQ4IqTVHm/
[9] => 1847
[likes] => 1847
[10] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg
[low_resolution] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg
[11] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg
[thumbnail] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg
[12] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg
[standard_resolution] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg
[13] => 223002407889359334_26499947
[pid] => 223002407889359334_26499947
[14] => doutzenkroes1
[username] => doutzenkroes1
[15] =>
[website] =>
[16] =>
[bio] =>
[17] => http://images.instagram.com/profiles/profile_26499947_75sq_1335638089.jpg
[profile_picture] => http://images.instagram.com/profiles/profile_26499947_75sq_1335638089.jpg
[18] => Doutzen Kroes
[full_name] => Doutzen Kroes
[19] => 26499947
[uid] => 26499947
)
Array
(
[0] => 101
[id] => 101
[1] =>
[attribution] =>
[2] => image
[type] => image
[3] => 13.827936172
[latitude] => 13.827936172
[4] => 100.471916198
[longitude] => 100.471916198
[5] => 20
[comments] => 20
[6] => Brannan
[filter] => Brannan
[7] => 1340802429
[created_time] => 1340802429
[8] => http://instagr.am/p/MYN62aqdE8/
[link] => http://instagr.am/p/MYN62aqdE8/
[9] => 1497
[likes] => 1497
[10] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_6.jpg
[low_resolution] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_6.jpg
[11] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_5.jpg
[thumbnail] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_5.jpg
[12] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_7.jpg
[standard_resolution] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_7.jpg
[13] => 222989400318529852_1387675
[pid] => 222989400318529852_1387675
[14] => wan_soloist
[username] => wan_soloist
[15] => http://www.facebook.com/WanThanakrit
[website] => http://www.facebook.com/WanThanakrit
[16] => Soloist SnapShot & Last Lemonade~!!
[bio] => Soloist SnapShot & Last Lemonade~!!
[17] => http://images.instagram.com/profiles/profile_1387675_75sq_1334119071.jpg
[profile_picture] => http://images.instagram.com/profiles/profile_1387675_75sq_1334119071.jpg
[18] => Wan Soloist
[full_name] => Wan Soloist
[19] => 1387675
[uid] => 1387675
)
Array
(
[0] => 100
[id] => 100
[1] =>
[attribution] =>
[2] => image
[type] => image
[3] =>
[latitude] =>
[4] =>
[longitude] =>
[5] => 25
[comments] => 25
[6] => Normal
[filter] => Normal
[7] => 1340804961
[created_time] => 1340804961
[8] => http://instagr.am/p/MYSwEjCzRq/
[link] => http://instagr.am/p/MYSwEjCzRq/
[9] => 1544
[likes] => 1544
[10] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_6.jpg
[low_resolution] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_6.jpg
[11] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_5.jpg
[thumbnail] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_5.jpg
[12] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_7.jpg
[standard_resolution] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_7.jpg
[13] => 223010649809826922_10380716
[pid] => 223010649809826922_10380716
[14] => amarelloo
[username] => amarelloo
[15] =>
[website] =>
[16] =>
[bio] =>
[17] => http://images.instagram.com/profiles/profile_10380716_75sq_1338715577.jpg
[profile_picture] => http://images.instagram.com/profiles/profile_10380716_75sq_1338715577.jpg
[18] => Marcelo Peña
[full_name] => Marcelo Peña
[19] => 10380716
[uid] => 10380716
)
Array
(
[0] => 99
[id] => 99
[1] =>
[attribution] =>
[2] => image
[type] => image
[3] =>
[latitude] =>
[4] =>
[longitude] =>
[5] => 43
[comments] => 43
[6] => Sutro
[filter] => Sutro
[7] => 1340799635
[created_time] => 1340799635
[8] => http://instagr.am/p/MYIlthLZhQ/
[link] => http://instagr.am/p/MYIlthLZhQ/
[9] => 1412
[likes] => 1412
[10] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_6.jpg
[low_resolution] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_6.jpg
[11] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_5.jpg
[thumbnail] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_5.jpg
[12] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_7.jpg
[standard_resolution] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_7.jpg
[13] => 222965957422585936_11252917
[pid] => 222965957422585936_11252917
[14] => stevesototattoo
[username] => stevesototattoo
[15] => http://www.stevesototattoo.com
[website] => http://www.stevesototattoo.com
[16] => Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin
[bio] => Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin
[17] => http://images.instagram.com/profiles/profile_11252917_75sq_1339634703.jpg
[profile_picture] => http://images.instagram.com/profiles/profile_11252917_75sq_1339634703.jpg
[18] => Steve Soto Tattoo Art Co.
[full_name] => Steve Soto Tattoo Art Co.
[19] => 11252917
[uid] => 11252917
)
Array
(
[0] => 98
[id] => 98
[1] =>
[attribution] =>
[2] => image
[type] => image
[3] =>
[latitude] =>
[4] =>
[longitude] =>
[5] => 83
[comments] => 83
[6] => Valencia
[filter] => Valencia
[7] => 1340800009
[created_time] => 1340800009
[8] => http://instagr.am/p/MYJTYbhVND/
[link] => http://instagr.am/p/MYJTYbhVND/
[9] => 1824
[likes] => 1824
[10] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_6.jpg
[low_resolution] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_6.jpg
[11] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_5.jpg
[thumbnail] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_5.jpg
[12] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_7.jpg
[standard_resolution] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_7.jpg
[13] => 222969095875023683_3745339
[pid] => 222969095875023683_3745339
[14] => namidacross
[username] => namidacross
[15] =>
[website] =>
[16] => Art Director / Graphic Designer // Japan
My another account
B&W only â–· #namidacrossbw
[bio] => Art Director / Graphic Designer // Japan
My another account
B&W only â–· #namidacrossbw
[17] => http://images.instagram.com/profiles/profile_3745339_75sq_1315794772.jpg
[profile_picture] => http://images.instagram.com/profiles/profile_3745339_75sq_1315794772.jpg
[18] => NAMiDA+
[full_name] => NAMiDA+
[19] => 3745339
[uid] => 3745339
)
And here's the code that I m using to fetch it and convert it to a valid json object so that my Javascript code can fetch it and parse it.
$query = mysql_query("SELECT * FROM `table_name` ORDER BY id DESC LIMIT 5") or die(mysql_error()); // fetching the latest 5 rows
while ($result = mysql_fetch_array($query)) {
//echo "<pre>"; print_r($result); echo "</pre>";
print (json_encode($result));
}
but the response that I get using json_encode doesnt seem to be a valid json object!
What am I doing wrong ?
Here's the json object that is emitted
{
"0": "102",
"id": "102",
"1": "",
"attribution": "",
"2": "image",
"type": "image",
"3": "",
"latitude": "",
"4": "",
"longitude": "",
"5": "81",
"comments": "81",
"6": "Nashville",
"filter": "Nashville",
"7": "1340803981",
"created_time": "1340803981",
"8": "http:\/\/instagr.am\/p\/MYQ4IqTVHm\/",
"link": "http:\/\/instagr.am\/p\/MYQ4IqTVHm\/",
"9": "1847",
"likes": "1847",
"10": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg",
"low_resolution": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg",
"11": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg",
"thumbnail": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg",
"12": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg",
"standard_resolution": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg",
"13": "223002407889359334_26499947",
"pid": "223002407889359334_26499947",
"14": "doutzenkroes1",
"username": "doutzenkroes1",
"15": "",
"website": "",
"16": "",
"bio": "",
"17": "http:\/\/images.instagram.com\/profiles\/profile_26499947_75sq_1335638089.jpg",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_26499947_75sq_1335638089.jpg",
"18": "Doutzen Kroes",
"full_name": "Doutzen Kroes",
"19": "26499947",
"uid": "26499947"
} {
"0": "101",
"id": "101",
"1": "",
"attribution": "",
"2": "image",
"type": "image",
"3": "13.827936172",
"latitude": "13.827936172",
"4": "100.471916198",
"longitude": "100.471916198",
"5": "20",
"comments": "20",
"6": "Brannan",
"filter": "Brannan",
"7": "1340802429",
"created_time": "1340802429",
"8": "http:\/\/instagr.am\/p\/MYN62aqdE8\/",
"link": "http:\/\/instagr.am\/p\/MYN62aqdE8\/",
"9": "1497",
"likes": "1497",
"10": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_6.jpg",
"low_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_6.jpg",
"11": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_5.jpg",
"thumbnail": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_5.jpg",
"12": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_7.jpg",
"standard_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_7.jpg",
"13": "222989400318529852_1387675",
"pid": "222989400318529852_1387675",
"14": "wan_soloist",
"username": "wan_soloist",
"15": "http:\/\/www.facebook.com\/WanThanakrit",
"website": "http:\/\/www.facebook.com\/WanThanakrit",
"16": "Soloist SnapShot & Last Lemonade~!!",
"bio": "Soloist SnapShot & Last Lemonade~!!",
"17": "http:\/\/images.instagram.com\/profiles\/profile_1387675_75sq_1334119071.jpg",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1387675_75sq_1334119071.jpg",
"18": "Wan Soloist",
"full_name": "Wan Soloist",
"19": "1387675",
"uid": "1387675"
} {
"0": "100",
"id": "100",
"1": "",
"attribution": "",
"2": "image",
"type": "image",
"3": "",
"latitude": "",
"4": "",
"longitude": "",
"5": "25",
"comments": "25",
"6": "Normal",
"filter": "Normal",
"7": "1340804961",
"created_time": "1340804961",
"8": "http:\/\/instagr.am\/p\/MYSwEjCzRq\/",
"link": "http:\/\/instagr.am\/p\/MYSwEjCzRq\/",
"9": "1544",
"likes": "1544",
"10": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_6.jpg",
"low_resolution": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_6.jpg",
"11": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_5.jpg",
"thumbnail": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_5.jpg",
"12": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_7.jpg",
"standard_resolution": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_7.jpg",
"13": "223010649809826922_10380716",
"pid": "223010649809826922_10380716",
"14": "amarelloo",
"username": "amarelloo",
"15": "",
"website": "",
"16": "",
"bio": "",
"17": "http:\/\/images.instagram.com\/profiles\/profile_10380716_75sq_1338715577.jpg",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_10380716_75sq_1338715577.jpg",
"18": "Marcelo Pe\u00f1a",
"full_name": "Marcelo Pe\u00f1a",
"19": "10380716",
"uid": "10380716"
} {
"0": "99",
"id": "99",
"1": "",
"attribution": "",
"2": "image",
"type": "image",
"3": "",
"latitude": "",
"4": "",
"longitude": "",
"5": "43",
"comments": "43",
"6": "Sutro",
"filter": "Sutro",
"7": "1340799635",
"created_time": "1340799635",
"8": "http:\/\/instagr.am\/p\/MYIlthLZhQ\/",
"link": "http:\/\/instagr.am\/p\/MYIlthLZhQ\/",
"9": "1412",
"likes": "1412",
"10": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_6.jpg",
"low_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_6.jpg",
"11": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_5.jpg",
"thumbnail": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_5.jpg",
"12": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_7.jpg",
"standard_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_7.jpg",
"13": "222965957422585936_11252917",
"pid": "222965957422585936_11252917",
"14": "stevesototattoo",
"username": "stevesototattoo",
"15": "http:\/\/www.stevesototattoo.com",
"website": "http:\/\/www.stevesototattoo.com",
"16": "Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin",
"bio": "Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin",
"17": "http:\/\/images.instagram.com\/profiles\/profile_11252917_75sq_1339634703.jpg",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_11252917_75sq_1339634703.jpg",
"18": "Steve Soto Tattoo Art Co.",
"full_name": "Steve Soto Tattoo Art Co.",
"19": "11252917",
"uid": "11252917"
} {
"0": "98",
"id": "98",
"1": "",
"attribution": "",
"2": "image",
"type": "image",
"3": "",
"latitude": "",
"4": "",
"longitude": "",
"5": "83",
"comments": "83",
"6": "Valencia",
"filter": "Valencia",
"7": "1340800009",
"created_time": "1340800009",
"8": "http:\/\/instagr.am\/p\/MYJTYbhVND\/",
"link": "http:\/\/instagr.am\/p\/MYJTYbhVND\/",
"9": "1824",
"likes": "1824",
"10": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_6.jpg",
"low_resolution": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_6.jpg",
"11": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_5.jpg",
"thumbnail": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_5.jpg",
"12": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_7.jpg",
"standard_resolution": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_7.jpg",
"13": "222969095875023683_3745339",
"pid": "222969095875023683_3745339",
"14": "namidacross",
"username": "namidacross",
"15": "",
"website": "",
"16": "Art Director \/ Graphic Designer \/\/ Japan\r\n\r\nMy another account \r\nB&W only \u25b7 #namidacrossbw\r\n",
"bio": "Art Director \/ Graphic Designer \/\/ Japan\r\n\r\nMy another account \r\nB&W only \u25b7 #namidacrossbw\r\n",
"17": "http:\/\/images.instagram.com\/profiles\/profile_3745339_75sq_1315794772.jpg",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3745339_75sq_1315794772.jpg",
"18": "NAMiDA+",
"full_name": "NAMiDA+",
"19": "3745339",
"uid": "3745339"
}
Your javascript probably receives something like {"id":1,...}{"id":2}{...}{...} instead of [{"id":1,...},{"id":2},{...},{...},....]
You should collect all rows into an array, and when you have all rows then encode the rows array:
$query = mysql_query("SELECT * FROM `table_name` ORDER BY id DESC LIMIT 5");
$rows = array();
while ($result = mysql_fetch_assoc($query)) {
$rows[] = $result;
}
echo json_encode( $rows );

Categories