使用php以json格式检索数据

时间:2016-02-21 19:23:32

标签: php json

我想使用php检索下面给出的json格式的数据。编写相同的代码是因为我尝试了很多但却无法做到。请帮帮我:

    <?php  

    $db=new PDO('mysql:dbname=punehack;host=localhost;','*******','********');  
    $row=$db->prepare('select * from gyms where live="yes" order by rand() LIMIT 2');  
    $row->execute();//execute the query  
    $json_data=array();//create the array
    $json_latdata=array();

      $result = array();
      $result1 = array();
       foreach($row as $rec)//foreach loop  
       {  
               $json_array['id']=$rec['id']; 
               $json_array['username']=$rec['username'];   
               $json_array['images']=$rec['images'];  
               $json_array['name']=$rec['name']; 
               $json_array['location']=$rec['location'];     
               $json_array['category1']=$rec['category1']; 
               $json_array['category2']=$rec['category2']; 
               $json_array['category3']=$rec['category3']; 
               $json_array['city']=$rec['city'];  
               $json_array['type']=$rec['type'];
               $json_array['male']=$rec['male'];
               $json_array['female']=$rec['female'];
               $json_array['fitdiary']=$rec['fitdiary'];
               $json_array1['latitude']=$rec['latitude'];  
               $json_array1['longitude']=$rec['longitude'];
               array_push($json_data,$json_array); 
              array_push($json_latdata,$json_array1);   
           }  
        $result = $json_data;
        $result1 = $json_latdata;
        echo json_encode(array($result,$result1));
          ?>

输出为:

[
    [{
        "id": "101",
        "username": "thefitnessfloor-shivajinagar",
        "images": "thefitnessfloor\/shivajinagar\/ambiance\/1.jpg",
        "name": "The Fitness Floor",
        "location": "Shivaji Nagar",
        "city": "Pune",
        "type": "Gym",
        "male": "yes",
        "female": "yes",
        "fitdiary": "yes"
    }, {
        "id": "97",
        "username": "bodyworks-wanowrie",
        "images": "bodyworks\/wanowrie\/ambiance\/1.jpg",
        "name": "Body Works",
        "location": "Wanowrie",
        "city": "Pune",
        "type": "Gym",
        "male": "yes",
        "female": "yes",
        "fitdiary": "yes"
    }],
    [{
        "latitude": "18.526776",
        "longitude": "73.843531"
    }, {
        "latitude": "18.481161",
        "longitude": "73.901035"
    }]
]

需要以下面给出的格式检索数据:

{
     "id": 1,
    "name": "The Flying Falafel",
    "username": "jhhkjhkhkh",
    "images": "bashfjdskjfksjdkj",
     "categories": [
         "category1",
         "category2",
         "category3"
    ],
    "location": "1051 Market St, SoMa, San Francisco, CA 94103",
    "city": "hjhjhjhjh",
    "type": "hjhjhjhjh",
    "male": "y",
    "female": "y",
    "coordinate": {
        "latitude": 37.78125,
        "longitude": -122.4113007
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

           $json_array['id']=$rec['id']; 
           $json_array['username']=$rec['username'];   
           $json_array['images']=$rec['images'];  
           $json_array['name']=$rec['name']; 
           $json_array['location']=$rec['location'];     
           $json_array['categories'][0]=$rec['category1']; 
           $json_array['categories'][1]=$rec['category2']; 
           $json_array['categories'][2]=$rec['category3']; 
           $json_array['city']=$rec['city'];  
           $json_array['type']=$rec['type'];
           $json_array['male']=$rec['male'];
           $json_array['female']=$rec['female'];
           $json_array['fitdiary']=$rec['fitdiary'];
           $json_array['coordinate']['latitude']=$rec['latitude'];  
           $json_array['coordinate']['longitude']=$rec['longitude'];

答案 1 :(得分:0)

你离得太远了。需要将坐标附加到原始数组,否则您将获得与其相应记录分离的所有坐标。

           ...
           $coordinates['latitude']=$rec['latitude'];  
           $coordinates['longitude']=$rec['longitude'];
           $json_data['coordinate'] = $coordinates;
           array_push($json_data,$json_array); 

答案 2 :(得分:0)

首先,在您的示例中,不存在categories

删除$json_data$json_latdata$result1声明:

$result = array();

foreach($row as $rec)//foreach loop  
{  

初始化$json_array(这不是强制性的,但这是一个好习惯):

    $json_array = array(); 
    $json_array['id']=$rec['id']; 
    $json_array['username']=$rec['username'];   
    $json_array['images']=$rec['images'];  
    $json_array['name']=$rec['name']; 
    $json_array['location']=$rec['location'];  
    $json_array['city']=$rec['city'];  
    $json_array['type']=$rec['type'];
    $json_array['male']=$rec['male'];
    $json_array['female']=$rec['female'];
    $json_array['fitdiary']=$rec['fitdiary'];

以这种方式创建子数组coordinate

    $json_array['coordinate'] = array();
    $json_array['coordinate']['latitude']=$rec['latitude'];
    $json_array['coordinate']['longitude']=$rec['longitude'];

将数组添加到$result(当只有一个项目附加到数组时,最好使用[]语法而不是array_push()):

    $result[] = $json_array; 
}  

最后,回显您的JSON编码结果:

echo json_encode( $result );
?>