issues when looping throw a json on php - php

this is driving me nuts,
I'm trying to loop throw a specific json:
I tried using
json_encode -> throws Warning: curl_close() expects parameter 1 to be resource, null given
json_decode -> shows the same text
$json->{"SKU"} and $json->SKU shows the same text
I tried to validate the json with different websites and all say the json is correct
I want to take only this values SKU, price, specialprice (with everything inside) and quantity
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
Thanks a lot for any hint (I already tried almost every answer on stack)

I'm pretty sure you can do it in this way:
<?php
//Enter your code here, enjoy!
$text = <<<EOT
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
EOT;
$decoded = json_decode($text);
# Index 0 because is an array
var_dump($decoded[0]->SKU);
You can execute the code directly here.
Hope this helps.

Related

The avalara JSON request not working using PHP cURL but working in postman

I have created a JSON for avalara request, line items are 10 in count. But it is always showing error as -
code:ErrorCountLimitExceededError
number:1722
message : The request has exceeded the maximum number of validation errors.
When I reduce the line item to count 6, it works fine. but my requirement is more than that.
The same JSON is working fine in postman.
Following is the JSON which I had used-
{
"lines": [
{
"number": "1",
"quantity": 1,
"amount": 100,
"taxCode": "PS081282",
"itemCode": "Y0001GHGH",
"description": "TEST"
},
{
"number": "2",
"quantity": 1,
"amount": 100,
"taxCode": "PS081282A",
"itemCode": "TTTTYYYA",
"description": "TEST"
},
{
"number": "3",
"quantity": 1,
"amount": 190,
"taxCode": "PS081282B",
"itemCode": "HGHGHG0001B",
"description": "TEST"
},
{
"number": "4",
"quantity": 1,
"amount": 2300,
"taxCode": "PS081282C",
"itemCode": "PKOO0001C",
"description": "TEST"
},
{
"number": "5",
"quantity": 1,
"amount": 200,
"taxCode": "PS081282D",
"itemCode": "ABCF567",
"description": "TEST"
},
{
"number": "6",
"quantity": 1,
"amount": 10990,
"taxCode": "PS081282E",
"itemCode": "Y00DSD01E",
"description": "TEST"
},
{
"number": "7",
"quantity": 1,
"amount": 140,
"taxCode": "PS081282F",
"itemCode": "Y00GJG232",
"description": "TEST"
},
{
"number": "8",
"quantity": 1,
"amount": 100,
"taxCode": "PS081282F2X",
"itemCode": "65HGFH675",
"description": "TEST"
}
],
"type": "SalesInvoice",
"companyCode": "DEFAULT",
"date": "2023-01-10",
"customerCode": "ABC",
"purchaseOrderNo": "2023-01-10-001",
"addresses": {
"singleLocation": {
"line1": "2000 Main Street",
"city": "Irvine",
"region": "CA",
"country": "US",
"postalCode": "92614"
}
},
"commit": true,
"currencyCode": "USD",
"description": "Yarn"
}

Count Json elements wiht PHP

So I have
{
"members": [
{
"username": "John",
"status": "offline",
"avatar_url": "...",
"id": "830232882252102064"
},
{
"username": "Momo",
"status": "online",
"avatar_url": "...",
"id": "259137993351102464"
}
]
}
How do I count (in php) how many users are offline and how many users are online and return them into a value like $memonline and $memoffline.
Here in below code we are getting values of all statuses and then we are counting values of all statuses.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='{
"channels": [
{
"position": 13,
"id": "304700935878213642",
"name": "20KBPS"
},
{
"position": 12,
"id": "304700895978061835",
"name": "50KBPS"
},
{
"position": 11,
"id": "304701193261809672",
"name": "70KBPS"
},
{
"position": 10,
"id": "304701326288224256",
"name": "90KBPS"
},
{
"position": 1,
"id": "304699877621891072",
"name": "=================="
},
{
"position": 9,
"id": "304700570592346114",
"name": "=================="
},
{
"position": 4,
"id": "304701407221514240",
"name": "=================="
},
{
"position": 14,
"id": "304700808883339264",
"name": "=================="
},
{
"position": 2,
"id": "304700525939523584",
"name": "Channel 1"
},
{
"position": 3,
"id": "304700547426942976",
"name": "Channel 2"
},
{
"position": 0,
"id": "304692483973971990",
"name": "General Channel"
},
{
"position": 6,
"id": "304701637446991873",
"name": "Private/2"
},
{
"position": 5,
"id": "304701480605319178",
"name": "Private/2"
},
{
"position": 7,
"id": "304701680010788866",
"name": "Private/3"
},
{
"position": 8,
"id": "304701738999611394",
"name": "Private/3"
},
{
"position": 15,
"id": "304700776691793921",
"name": "Trash Bin"
}
],
"instant_invite": null,
"id": "304692483973971989",
"members": [
{
"username": "Dyno",
"status": "online",
"bot": true,
"game": {
"name": "dynobot.net | ?help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/155149108183695360/5aeb68c29b56b3d92eddb6f46df5051c.jpg",
"avatar": "5aeb68c29b56b3d92eddb6f46df5051c",
"discriminator": "3861",
"id": "155149108183695360"
},
{
"username": "Momo",
"status": "online",
"bot": true,
"game": {
"name": "$help | $info"
},
"avatar_url": "https://cdn.discordapp.com/avatars/259137993351102464/a3005ab7aff3eb829fecf375931a76f1.jpg",
"avatar": "a3005ab7aff3eb829fecf375931a76f1",
"discriminator": "4649",
"id": "259137993351102464"
},
{
"username": "Ronny Dark",
"status": "online",
"avatar_url": "https://cdn.discordapp.com/avatars/152855546574143492/6d9b98972ca6f0308be4dd2aec5aaca3.jpg",
"avatar": "6d9b98972ca6f0308be4dd2aec5aaca3",
"discriminator": "1480",
"id": "152855546574143492"
}
],
"name": "Server name"
}';
$array=json_decode($string,true);
$result=array_column($array["members"],"status");
$members=array_count_values($result);
echo isset($members["offline"]) ? $members["offline"] : 0;

Sorting only a part of object/array in PHP

I've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}

Get the values in PHP of three objects in Tripadvisor JSON API file

Been struggling with this for too long now, so am kindly asking for your help.
How can I, using PHP, get the values of the text fields in the reviews of which there are three in this JSON file below.
Want to use a foreach loop for this, thanks for helping me out!
{
"address_obj": {
"street1": "Rustenburgerstreet 384",
"street2": null,
"city": "Amsterdam",
"state": "North Holland Province",
"country": "The Netherlands",
"postalcode": "1072 HG",
"address_string": "Rustenburgerstreet 384, 1072 HG Amsterdam The Netherlands"
},
"percent_recommended": null,
"latitude": "52.35162",
"rating": "5.0",
"attraction_types": [
{
"name": "concerts",
"localized_name": "Concerts"
},
{
"name": "blues bars",
"localized_name": "Blues Bars"
},
{
"name": "jazz bars",
"localized_name": "Jazz Bars"
},
{
"name": "bar/ clubs",
"localized_name": "Bars & Clubs"
}
],
"wikipedia_info": null,
"location_id": "3724036",
"review_rating_count": {
"1": "0",
"2": "0",
"3": "1",
"4": "4",
"5": "35"
},
"ranking_data": {
"ranking_string": "#12 of 73 Theater & Concerts in Amsterdam",
"ranking_out_of": "73",
"geo_location_id": "188590",
"ranking": "12",
"geo_location_name": "Amsterdam"
},
"photo_count": "35",
"location_string": "Amsterdam, North Holland Province",
"trip_types": [
{
"name": "business",
"value": "0",
"localized_name": "Business"
},
{
"name": "couples",
"value": "8",
"localized_name": "Couples"
},
{
"name": "solo",
"value": "7",
"localized_name": "Solo travel"
},
{
"name": "family",
"value": "0",
"localized_name": "Family"
},
{
"name": "friends",
"value": "21",
"localized_name": "Friends getaway"
}
],
"web_url": "Attraction_Review-g188590-d3724036-Reviews-m34757-CC_Music_Cafe-Amsterdam_North_Holland_Province.html",
"reviews": [
{
"id": "353301385",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-03-06T05:20:19-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r353301385-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review353301385",
"trip_type": "Solo travel",
"travel_date": "2016-02",
"text": "I am a regular visitor of CC Muziekcafé Amsterdam but have felt at home from the very first time. What I like about CC is the atmosphere where great music and hospitality are mixed in the best way...",
"user": {
"username": "Yvon H",
"user_location": {
"name": "Groningen Province, The Netherlands",
"id": "188570"
}
},
"title": "A great place to hear live music and meet all sorts of interesting people, both local and traveling",
"is_machine_translated": false
},
{
"id": "351658487",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-28T11:13:12-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r351658487-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review351658487",
"trip_type": "Friends getaway",
"travel_date": "2016-02",
"text": "4th time we have been here, another great night at the music cafe, friendly people and a barman who knows how to just put enough swear words in to sound cool",
"user": {
"username": "Francois S",
"user_location": {
"name": "Cardiff, United Kingdom",
"id": "186460"
}
},
"title": "Jazz funk Jam session night Thursday",
"is_machine_translated": false
},
{
"id": "350605184",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-24T10:18:57-0500",
"rating": 5,
"helpful_votes": "1",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r350605184-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review350605184",
"trip_type": "Couples",
"travel_date": "2015-09",
"text": "CC muziekcafe is a very cosy place with excellent live music and interaction with the musicians. What really makes the place is the owner Rene who knows a lot about music and now and then even sings...",
"user": {
"username": "ImagineNL",
"user_location": {
"name": "Schagen, The Netherlands",
"id": "609049"
}
},
"title": "Cupid",
"is_machine_translated": false
}
],
You can use this code
<?php
$json = <<<EOF
{
"address_obj": {
"street1": "Rustenburgerstreet 384",
"street2": null,
"city": "Amsterdam",
"state": "North Holland Province",
"country": "The Netherlands",
"postalcode": "1072 HG",
"address_string": "Rustenburgerstreet 384, 1072 HG Amsterdam The Netherlands"
},
"percent_recommended": null,
"latitude": "52.35162",
"rating": "5.0",
"attraction_types": [
{
"name": "concerts",
"localized_name": "Concerts"
},
{
"name": "blues bars",
"localized_name": "Blues Bars"
},
{
"name": "jazz bars",
"localized_name": "Jazz Bars"
},
{
"name": "bar/ clubs",
"localized_name": "Bars & Clubs"
}
],
"wikipedia_info": null,
"location_id": "3724036",
"review_rating_count": {
"1": "0",
"2": "0",
"3": "1",
"4": "4",
"5": "35"
},
"ranking_data": {
"ranking_string": "#12 of 73 Theater & Concerts in Amsterdam",
"ranking_out_of": "73",
"geo_location_id": "188590",
"ranking": "12",
"geo_location_name": "Amsterdam"
},
"photo_count": "35",
"location_string": "Amsterdam, North Holland Province",
"trip_types": [
{
"name": "business",
"value": "0",
"localized_name": "Business"
},
{
"name": "couples",
"value": "8",
"localized_name": "Couples"
},
{
"name": "solo",
"value": "7",
"localized_name": "Solo travel"
},
{
"name": "family",
"value": "0",
"localized_name": "Family"
},
{
"name": "friends",
"value": "21",
"localized_name": "Friends getaway"
}
],
"web_url": "Attraction_Review-g188590-d3724036-Reviews-m34757-CC_Music_Cafe-Amsterdam_North_Holland_Province.html",
"reviews": [
{
"id": "353301385",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-03-06T05:20:19-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r353301385-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review353301385",
"trip_type": "Solo travel",
"travel_date": "2016-02",
"text": "I am a regular visitor of CC Muziekcafé Amsterdam but have felt at home from the very first time. What I like about CC is the atmosphere where great music and hospitality are mixed in the best way...",
"user": {
"username": "Yvon H",
"user_location": {
"name": "Groningen Province, The Netherlands",
"id": "188570"
}
},
"title": "A great place to hear live music and meet all sorts of interesting people, both local and traveling",
"is_machine_translated": false
},
{
"id": "351658487",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-28T11:13:12-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r351658487-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review351658487",
"trip_type": "Friends getaway",
"travel_date": "2016-02",
"text": "4th time we have been here, another great night at the music cafe, friendly people and a barman who knows how to just put enough swear words in to sound cool",
"user": {
"username": "Francois S",
"user_location": {
"name": "Cardiff, United Kingdom",
"id": "186460"
}
},
"title": "Jazz funk Jam session night Thursday",
"is_machine_translated": false
},
{
"id": "350605184",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-24T10:18:57-0500",
"rating": 5,
"helpful_votes": "1",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r350605184-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review350605184",
"trip_type": "Couples",
"travel_date": "2015-09",
"text": "CC muziekcafe is a very cosy place with excellent live music and interaction with the musicians. What really makes the place is the owner Rene who knows a lot about music and now and then even sings...",
"user": {
"username": "ImagineNL",
"user_location": {
"name": "Schagen, The Netherlands",
"id": "609049"
}
},
"title": "Cupid",
"is_machine_translated": false
}
]
}
EOF;
$array = json_decode($json, true);
$texts = array_map(
function($item) {
return $item['text'];
}, $array['reviews']
);
It doesn't seem like you need a foreach to fetch the reviews element. Not sure if I understood your question correct, but did you want something like this:
$assoc_json = json_decode($your_json, true);
var_dump($assoc_json['reviews']);
The above turns your json into an associative array, and just access the review element.
A simple, naive approach would be:
//Loads your json file 'tripadvisor' into jsonString
$jsonString = file_get_contents("/tripadvisor.json");
//Turns your string into an associative array
$tripJsonAssoc = json_decode($jsonString, true);
//Iterate through each review and store it in result
$result = array();
foreach($tripJsonAssoc['reviews'] as $review) {
$result[] = array('text' => $review['text'],
'rating' => $review['rating']);
}
//Do what you need to do with result
//...
Or you can just do your own stuff inside the foreach loop.
This answer gives you another way (more elegant) to create the 'result' array, but since you asked for a 'foreach', I thought I would add my 5 cents.

Extracting values from JSON data

I have the JSON data given below. The 'crop' array contains multiple array of crop price, I want to get average crop price of all districts(using 'district_id').
<i>{
"status": "ok",
"count": 7,
"crop": [
{
"id": "133",
"crop_id": "81",
"price": "45.00",
"description": null,
"user_id": "27",
"block_id": "1",
"longitude": "87.8661808",
"latitude": "23.2340073",
"date": "2014-02-03",
"time": "12:44:43",
"district_id": "1"
},
{
"id": "135",
"crop_id": "81",
"price": "10.50",
"description": null,
"user_id": "27",
"block_id": "1",
"longitude": "87.8662402",
"latitude": "23.2339822",
"date": "2014-02-03",
"time": "12:44:54",
"district_id": "1"
},
{
"id": "143",
"crop_id": "81",
"price": "35",
"description": null,
"user_id": "27",
"block_id": "1",
"longitude": "0.0",
"latitude": "0.0",
"date": "2014-02-03",
"time": "13:12:50",
"district_id": "1"
},
{
"id": "146",
"crop_id": "81",
"price": "85",
"description": null,
"user_id": "27",
"block_id": "1",
"longitude": "0.0",
"latitude": "0.0",
"date": "2014-02-03",
"time": "14:29:07",
"district_id": "1"
},
{
"id": "148",
"crop_id": "81",
"price": "25",
"description": null,
"user_id": "27",
"block_id": "1",
"longitude": "0.0",
"latitude": "0.0",
"date": "2014-02-03",
"time": "14:58:01",
"district_id": "1"
},
{
"id": "132",
"crop_id": "81",
"price": "10",
"description": null,
"user_id": "119",
"block_id": "34",
"longitude": "0.0",
"latitude": "0.0",
"date": "2014-02-03",
"time": "12:41:49",
"district_id": "4"
},
{
"id": "134",
"crop_id": "81",
"price": "12",
"description": null,
"user_id": "119",
"block_id": "34",
"longitude": "0.0",
"latitude": "0.0",
"date": "2014-02-03",
"time": "12:43:50",
"district_id": "4"
}
]
}</i>
Use json_decode method to convert it to an std_class or use the 2nd parameter of this method to convert it to an associated array.
Take a look at http://in1.php.net/json_decode
Once you decode it, you can easily access the variables like accessing properties of a class or as an array object if you are converting to an associated array
First you have too decode the JSON using json_decode function, then access using variables.
Do like this..
//Assign your JSON data to this $json variable as shown in the demo.
$arr = json_decode($json,1);
foreach($arr['crop'] as $arr1)
{
foreach($arr1 as $k=>$v)
{
if($arr1['district_id']==1)
{
$avgpr[] = $arr1['price'];
}
}
}
echo $avgprice = array_sum($avgpr)/count($avgpr);
OUTPUT :
40.1
Demo

Categories