Extracting values from JSON data - php

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

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"
}

how to do pagination complex array in cakephp rest-api?

CakePHP Rest-API pagination in an array of array. Like, containable model pagination in CakePHP for reducing API load.
Some existing code :
$this->request->data['page'];
$data = $this->Model1->find('all',array(
'contain' => array('Model2'),
'fields' => array(),
'conditions' => array('Model1.status' => 1 )
));
Below API response and I want to paginate in ProductUser array because when lots of product load at one-time app will be hang. It's possible or any other way to reduce data load in CakePHP.
API Response :
"status": true,
"message": "Available devices!",
"data": [
{
"id": "6",
"name": "Television",
"parent_id": "0",
"image": "",
"status": "1",
"created": "2019-06-10 15:22:44",
"modified": "2019-06-10 15:22:44",
"ProductUser": [ //Paginate this array
{
"id": "1",
"user_id": "17",
"category_id": "6",
"brand_id": "1",
"product_id": "17",
"date_of_purchase": "2019-06-22",
"seller_id": "1",
"warranty_year": "0",
"warranty_month": "0",
"warranty_end_date": null,
"purchese_price": null,
"bill_upload": "",
"emi_year": "0",
"emi_month": "0",
"emi_start_date": null,
"emi_end_date": null,
"insurance_company_id": "0",
"insurance_year": "0",
"insurance_month": "0",
"insurance_end_date": null,
"created": "2019-06-22 14:13:01",
"modified": "2019-06-22 14:17:07",
"Product": {
"id": "17",
"category_id": "6",
"brand_id": "1",
"product_code": "Samsung LED TV 39 inch",
"name": "Samsung LED TV 39 inch",
"selling_price": "13000",
"short_description": "Television",
"image": "catalog\/sam39.jpg",
"status": "1",
"created": "2019-05-14 00:00:00",
"modified": "2019-05-14 00:00:00"
}
},
{
"id": "1",
"user_id": "17",
"category_id": "6",
"brand_id": "1",
"product_id": "17",
"date_of_purchase": "2019-06-22",
"seller_id": "1",
"warranty_year": "0",
"warranty_month": "0",
"warranty_end_date": null,
"purchese_price": null,
"bill_upload": "",
"emi_year": "0",
"emi_month": "0",
"emi_start_date": null,
"emi_end_date": null,
"insurance_company_id": "0",
"insurance_year": "0",
"insurance_month": "0",
"insurance_end_date": null,
"created": "2019-06-22 14:13:01",
"modified": "2019-06-22 14:17:07",
"Product": {
"id": "17",
"category_id": "6",
"brand_id": "1",
"product_code": "Samsung LED TV 39 inch",
"name": "Samsung LED TV 39 inch",
"selling_price": "13000",
"short_description": "Television",
"image": "catalog\/sam39.jpg",
"status": "1",
"created": "2019-05-14 00:00:00",
"modified": "2019-05-14 00:00:00"
}
}
]
}]

How to replace null with " " double quotes for json output in php

I need help, we have already developed 500+ web-services for the current project, i don't want to modify all the web-services, can any help me how to replace null with " " double quotes for json output in php using output buffer..
Actual Output came
{
"err_code": "valid",
"title": "",
"message": "User details",
"data": {
"fullname": "Vijay Kumarr",
"mobile": "4564564564654",
"token": "OGUBXtyxDARNISJGOlQu",
"customer_meta": {
"id": "6",
"customers_id": "6",
"email": "xyz#abc.com",
"alternative_mobile": null,
"door_no": "1-2/3",
"street_name": "Street",
"location_name": "Locationn",
"city_name": "vizag",
"district_name": "Vizag",
"state_name": "Andhra Pradesh",
"zipcode": "530016",
"countries_id": "1",
"gst_number": null,
"aadhar_number": null,
"pan_number": null,
"created_at": "1522060789",
"updated_at": "1522484859",
"status": "1"
}
}
}
I am expecting Output as
{
"err_code": "valid",
"title": "",
"message": "User details",
"data": {
"fullname": "Vijay Kumarr",
"mobile": "4564564564654",
"token": "OGUBXtyxDARNISJGOlQu",
"customer_meta": {
"id": "6",
"customers_id": "6",
"email": "xyz#abc.com",
"alternative_mobile": "",
"door_no": "1-2/3",
"street_name": "Street",
"location_name": "Locationn",
"city_name": "vizag",
"district_name": "Vizag",
"state_name": "Andhra Pradesh",
"zipcode": "530016",
"countries_id": "1",
"gst_number": "",
"aadhar_number": "",
"pan_number": "",
"created_at": "1522060789",
"updated_at": "1522484859",
"status": "1"
}
}
}

issues when looping throw a json on 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.

Issue with json_decode and json_encode - it adds keys to original JSON

I have this JSON string:
{
"product": [
{
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "1"
},
{
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "2"
}
]
}
When I use the PHP function json_decode($products, true), and then I re-encode it using json_encode($products), the string becomes this:
{
"product": {
"1": {
"id": "2",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "2"
},
"2": {
"id": "1",
"title": "producta",
"size": "50",
"weight": "1000",
"price": "30",
"quantity": "100",
"cartID": "3"
}
}
}
After decoding and re-encoding, it adds a key to every "product"
Is there a way around this?
Posting this here since it'd be too ugly in a comment, but this is what I get on PHP 5.3.6 after doing a echo json_encode(json_decode('...your json...', true));:
{"product": [
{"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"1"},
{"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"2"}
]}
Note the lack of extra keys. Are you doing any manipulations on the decoed array before re-encoding?
You are probably using mysqli_fetch_array instead of mysqli_fetch_assoc. Meaning u are json decoding a key arreay instead of an associative array

Categories