是否可以使用where子句声明选择json数据?

时间:2016-01-04 05:54:30

标签: php json sorting

我的JSON数据存在问题。我有一个这样的字符串:

  data: [
    {
     groupid: "1449550897156TWTJ3UJNUR0CPTQ",
        voting_id: "14510478525898WTJUQX2FEJF0BU",
     answer_name: "081911112222",
     answer_code: "1",
     answer_content: "Hoka2Bento",
     datetime: "2015-12-25 19:51:12.075"
     },
     {
     groupid: "1449550897156TWTJ3UJNUR0CPTQ",
     voting_id: "14510478525898WTJUQX2FEJF0BU",
     answer_name: "085718733340",
     answer_code: "0",
     answer_content: "Hanamasa",
     datetime: "2015-12-25 19:52:42.862"
     },
     {
     groupid: "1449550897156TWTJ3UJNUR0CPTQ",
     voting_id: "14510478525898WTJUQX2FEJF0BU",
     answer_name: "081911113333",
     answer_code: "1",
     answer_content: "Hoka2Bento",
     datetime: "2015-12-25 20:19:51.643"
     },
     {
     groupid: "1449550897156TWTJ3UJNUR0CPTQ",
     voting_id: "14510478525898WTJUQX2FEJF0BU",
     answer_name: "087815254524",
     answer_code: "2",
     answer_content: "Korean Barbeque",
     datetime: "2015-12-25 20:30:46.418"
     }
     ],

我想使用参数count选择answer_name = 1数据。我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

请运行此代码,

<?php    
$data = '[
        {
         "groupid": "1449550897156TWTJ3UJNUR0CPTQ",
            "voting_id": "14510478525898WTJUQX2FEJF0BU",
         "answer_name": "081911112222",
         "answer_code": "1",
         "answer_content": "Hoka2Bento",
         "datetime": "2015-12-25 19:51:12.075"
         },
         {
         "groupid": "1449550897156TWTJ3UJNUR0CPTQ",
         "voting_id": "14510478525898WTJUQX2FEJF0BU",
         "answer_name": "085718733340",
         "answer_code": "0",
         "answer_content": "Hanamasa",
         "datetime": "2015-12-25 19:52:42.862"
         },
         {
         "groupid": "1449550897156TWTJ3UJNUR0CPTQ",
         "voting_id": "14510478525898WTJUQX2FEJF0BU",
         "answer_name": "081911113333",
         "answer_code": "1",
         "answer_content": "Hoka2Bento",
         "datetime": "2015-12-25 20:19:51.643"
         },
         {
         "groupid": "1449550897156TWTJ3UJNUR0CPTQ",
         "voting_id": "14510478525898WTJUQX2FEJF0BU",
         "answer_name": "087815254524",
         "answer_code": "2",
         "answer_content": "Korean Barbeque",
         "datetime": "2015-12-25 20:30:46.418"
         }
         ]';

        $array=json_decode($data);
        $count = 0;
       foreach($array as $arraydata)
       {
           if($arraydata->answer_code==1){
                $count++;
           }
       }

       echo "Count is" .($count);
 ?>     

这样,您将获得代码为= 1

的所有答案计数

如果您需要进一步的帮助,请告诉我。

由于 阿米特

答案 1 :(得分:0)

试试这个,解码php中的json数据。

 $array=json_decode($data);

   foreach($array as $arraydata)
   {
    $count[] = $arraydata['answer_name'];
   }

   echo "Count is" .count($count);