从JSON数据中提取值

时间:2014-02-04 05:53:36

标签: php arrays json

我有下面给出的JSON数据。 '作物'数组包含多个作物价格数组,我想获得所有地区的平均作物价格(使用' 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>

3 个答案:

答案 0 :(得分:2)

使用json_decode方法将其转换为std_class或使用此方法的第二个参数将其转换为关联的数组。

查看http://in1.php.net/json_decode

解码后,如果要转换为关联数组,则可以轻松访问变量,如访问类的属性或数组对象

答案 1 :(得分:0)

这样做..

//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

答案 2 :(得分:0)

首先,您使用json_decode函数解码JSON,然后使用变量进行访问。