0

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>
1
  • How about decode your json first using json_decode func.? Commented Feb 4, 2014 at 5:55

3 Answers 3

2

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 https://www.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

Sign up to request clarification or add additional context in comments.

Comments

0

First you have too decode the JSON using json_decode function, then access using variables.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.