json encode issues in php - php

Given this array:
Array
(
[0] => Array
(
[item_id] => 53
[item_name] => MRA Restaurant
[category_name] => Restaurants
[location_name] => Markhiya
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => MRA Restaurant 01.jpg
[item_image_location] => /images/item/
)
[1] => Array
(
[item_id] => 1
[item_name] => MRA Restaurant Bakery & Sweets
[category_name] => Restaurants
[location_name] => Al Ghanim
[item_description] => Great Kerala-style food and lunch buffet.
[item_comments_count] => 2
[item_likes_count] => 6
[item_image_file] => MRA Restaurant Bakery & Sweets.jpg
[item_image_location] => /images/item/
)
[2] => Array
(
[item_id] => 1486
[item_name] => Al Amrah Palace Cafeteria
[category_name] => Restaurants
[location_name] => Al Doha Al Jadeeda
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Al Amrah Palace Cafeteria.jpg
[item_image_location] => /images/item/
)
[3] => Array
(
[item_id] => 402
[item_name] => Al Hamra
[category_name] => Restaurants
[location_name] => Musheireb
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Al Hamra.jpg
[item_image_location] => /images/item/
)
[4] => Array
(
[item_id] => 2902
[item_name] => Al Meera Fereej Bin Omran
[category_name] => malls & hypermarkets
[location_name] => Doha
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Al Meera Fereej Bin Omran.jpg
[item_image_location] => /images/item/
)
[5] => Array
(
[item_id] => 1150
[item_name] => Al Omraa Patisserie & Sweets
[category_name] => Restaurants
[location_name] => Markhiya
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Al Omraa Patisserie & Sweets.jpg
[item_image_location] => /images/item/
)
)
When convert to JSON using the code:
$items=array_unique($items,SORT_REGULAR);
header('Content-Type: application/json');
echo json_encode($items);
The output is:
[
{
"item_id": "53",
"item_name": "MRA Restaurant",
"category_name": "Restaurants",
"location_name": "Markhiya",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "MRA Restaurant 01.jpg",
"item_image_location": "/images/item/"
},
{
"item_id": "1",
"item_name": "MRA Restaurant Bakery & Sweets",
"category_name": "Restaurants",
"location_name": "Al Ghanim",
"item_description": "Great Kerala-style food and lunch buffet.",
"item_comments_count": "5",
"item_likes_count": "7",
"item_image_file": "MRA Restaurant Bakery & Sweets.jpg",
"item_image_location": "/images/item/"
},
{
"item_id": "1486",
"item_name": "Al Amrah Palace Cafeteria",
"category_name": "Restaurants",
"location_name": "Al Doha Al Jadeeda",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Al Amrah Palace Cafeteria.jpg",
"item_image_location": "/images/item/"
},
{
"item_id": "402",
"item_name": "Al Hamra",
"category_name": "Restaurants",
"location_name": "Musheireb",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Al Hamra.jpg",
"item_image_location": "/images/item/"
},
{
"item_id": "2902",
"item_name": "Al Meera Fereej Bin Omran",
"category_name": "malls & hypermarkets",
"location_name": "Doha",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Al Meera Fereej Bin Omran.jpg",
"item_image_location": "/images/item/"
},
{
"item_id": "1150",
"item_name": "Al Omraa Patisserie & Sweets",
"category_name": "Restaurants",
"location_name": "Markhiya",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Al Omraa Patisserie & Sweets.jpg",
"item_image_location": "/images/item/"
}
]
This is working fine, but in the following array:
Array
(
[0] => Array
(
[item_id] => 2
[item_name] => Aalishan
[category_name] => Restaurants
[location_name] => Al Muntazah
[item_description] => Authentic North Indian dishes at reasonable prices.
[item_comments_count] => 0
[item_likes_count] => 6
[item_image_file] => aalishan-al-muntazah 03.jpg
[item_image_location] => /images/item/
)
[1] => Array
(
[item_id] => 403
[item_name] => Aalishan
[category_name] => Restaurants
[location_name] => Westbay
[item_description] =>
[item_comments_count] => 0
[item_likes_count] => 2
[item_image_file] => Abdul Khader Alhallab sweets.jpg
[item_image_location] => /images/item/
)
[4] => Array
(
[item_id] => 844
[item_name] => Jaal Broasted Chicken
[category_name] => Restaurants
[location_name] => Fereej Bin Mahmoud
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Jaal Broasted Chicken.jpg
[item_image_location] => /images/item/
)
[5] => Array
(
[item_id] => 1140
[item_name] => Jaal Cafeteria
[category_name] => Restaurants
[location_name] => Muaither
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Jaal Cafeteria.jpg
[item_image_location] => /images/item/
)
[6] => Array
(
[item_id] => 627
[item_name] => Jaal Restaurant
[category_name] => Restaurants
[location_name] => Musheireb
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => Jaal Restaurant.jpg
[item_image_location] => /images/item/
)
[7] => Array
(
[item_id] => 1422
[item_name] => Jaal Restaurant
[category_name] => Restaurants
[location_name] => Muaither
[item_description] =>
[item_comments_count] => 0
[item_likes_count] =>
[item_image_file] => 3716_2015_05_29_10_15_50.jpg
[item_image_location] => /images/item/
)
)
When encoded, the output is:
{
"0": {
"item_id": "2",
"item_name": "Aalishan",
"category_name": "Restaurants",
"location_name": "Al Muntazah",
"item_description": "Authentic North Indian dishes at reasonable prices.",
"item_comments_count": "1",
"item_likes_count": "6",
"item_image_file": "aalishan-al-muntazah 03.jpg",
"item_image_location": "/images/item/"
},
"1": {
"item_id": "403",
"item_name": "Aalishan",
"category_name": "Restaurants",
"location_name": "Westbay",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": "2",
"item_image_file": "Abdul Khader Alhallab sweets.jpg",
"item_image_location": "/images/item/"
},
"4": {
"item_id": "3307",
"item_name": "Faalin baby shop",
"category_name": "brands & Fashions",
"location_name": "Al Mirqab Al Jadeed Doha",
"item_description": "Exclusive kids showroom",
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "4045_2015_09_16_07_26_53.jpg",
"item_image_location": "/images/item/"
},
"5": {
"item_id": "844",
"item_name": "Jaal Broasted Chicken",
"category_name": "Restaurants",
"location_name": "Fereej Bin Mahmoud",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Jaal Broasted Chicken.jpg",
"item_image_location": "/images/item/"
},
"6": {
"item_id": "1140",
"item_name": "Jaal Cafeteria",
"category_name": "Restaurants",
"location_name": "Muaither",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Jaal Cafeteria.jpg",
"item_image_location": "/images/item/"
},
"7": {
"item_id": "627",
"item_name": "Jaal Restaurant",
"category_name": "Restaurants",
"location_name": "Musheireb",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "Jaal Restaurant.jpg",
"item_image_location": "/images/item/"
},
"8": {
"item_id": "1422",
"item_name": "Jaal Restaurant",
"category_name": "Restaurants",
"location_name": "Muaither",
"item_description": null,
"item_comments_count": "0",
"item_likes_count": null,
"item_image_file": "3716_2015_05_29_10_15_50.jpg",
"item_image_location": "/images/item/"
}
}
What is the issue behind this encoding of same type array but different output?

The array that gives you faulty output has missing indices. It goes from 1 to 4 and skips 2 and 3. That is why the JSON parser dismisses it as a regular array and starts treating it as an associative array.
This is due to array_unique preserving the keys and not re-indexing. array_values() will re-index the array for you:
echo json_encode(array_values($items));

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 can I decode angular.callbacks json

How can I decode this thank you in advance.
angular.callbacks._1({"transaction":{"token":"Lf8jxwKp3XwSTIhpmOvFbJiHL4l","created_at":"2019-11-01T11:25:02Z","updated_at":"2019-11-01T11:25:02Z","succeeded":true,"transaction_type":"AddPaymentMethod","retained":false,"message":"Succeeded!","message_key":"messages.transaction_succeeded","payment_method":{"token":"58ta8FWrvwiPlEjnwDrUvzYJSir","data":null,"metadata":null,"storage_state":"cached","email":"Iokp#iephmail.com","created_at":"2019-11-01T11:25:02Z","updated_at":"2019-11-01T11:25:02Z","errors":[],"payment_method_type":"credit_card","first_name":"Mansio","last_name":"Enioms","full_name":"Fredrick Erispe","card_type":"visa","last_four_digits":"0373","first_six_digits":"446542","month":10,"year":2025,"address1":"1961 Timbercrest Road","address2":"","city":"MARCUS","state":"IL","zip":"51035","country":"US","phone_number":"18089648560","eligible_for_card_updater":true,"shipping_address1":null,"shipping_address2":null,"shipping_city":null,"shipping_state":null,"shipping_zip":null,"shipping_country":null,"shipping_phone_number":null,"company":null,"verification_value":"XXX","number":"XXXX-XXXX-XXXX-0373","fingerprint":"a84a3e186cc102c5621c59db43dbd8522d69"}},"status":201});
My code
$json= json_decode($data, true);
echo $transaction = $json['transaction']['payment_method'];
It show nothing please help me.
You are not getting any results because your json is not valid. Json_decode function accept only valid json.
Like this:
<?php
$data = '{
"transaction": {
"token": "Lf8jxwKp3XwSTIhpmOvFbJiHL4l",
"created_at": "2019-11-01T11:25:02Z",
"updated_at": "2019-11-01T11:25:02Z",
"succeeded": true,
"transaction_type": "AddPaymentMethod",
"retained": false,
"message": "Succeeded!",
"message_key": "messages.transaction_succeeded",
"payment_method": {
"token": "58ta8FWrvwiPlEjnwDrUvzYJSir",
"data": null,
"metadata": null,
"storage_state": "cached",
"email": "Iokp#iephmail.com",
"created_at": "2019-11-01T11:25:02Z",
"updated_at": "2019-11-01T11:25:02Z",
"errors": [],
"payment_method_type": "credit_card",
"first_name": "Mansio",
"last_name": "Enioms",
"full_name": "Fredrick Erispe",
"card_type": "visa",
"last_four_digits": "0373",
"first_six_digits": "446542",
"month": 10,
"year": 2025,
"address1": "1961 Timbercrest Road",
"address2": "",
"city": "MARCUS",
"state": "IL",
"zip": "51035",
"country": "US",
"phone_number": "18089648560",
"eligible_for_card_updater": true,
"shipping_address1": null,
"shipping_address2": null,
"shipping_city": null,
"shipping_state": null,
"shipping_zip": null,
"shipping_country": null,
"shipping_phone_number": null,
"company": null,
"verification_value": "XXX",
"number": "XXXX-XXXX-XXXX-0373",
"fingerprint": "a84a3e186cc102c5621c59db43dbd8522d69"
}
},
"status": 201
}';
$json= json_decode($data, true);
print_r($json["transaction"]["payment_method"]);
Output will be:
Array
(
[token] => 58ta8FWrvwiPlEjnwDrUvzYJSir
[data] =>
[metadata] =>
[storage_state] => cached
[email] => Iokp#iephmail.com
[created_at] => 2019-11-01T11:25:02Z
[updated_at] => 2019-11-01T11:25:02Z
[errors] => Array
(
)
[payment_method_type] => credit_card
[first_name] => Mansio
[last_name] => Enioms
[full_name] => Fredrick Erispe
[card_type] => visa
[last_four_digits] => 0373
[first_six_digits] => 446542
[month] => 10
[year] => 2025
[address1] => 1961 Timbercrest Road
[address2] =>
[city] => MARCUS
[state] => IL
[zip] => 51035
[country] => US
[phone_number] => 18089648560
[eligible_for_card_updater] => 1
[shipping_address1] =>
[shipping_address2] =>
[shipping_city] =>
[shipping_state] =>
[shipping_zip] =>
[shipping_country] =>
[shipping_phone_number] =>
[company] =>
[verification_value] => XXX
[number] => XXXX-XXXX-XXXX-0373
[fingerprint] => a84a3e186cc102c5621c59db43dbd8522d69
)
Note: your valid json resides inside angular.callbacks._1

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

normal Array to nested Json Conversion 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);

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