返回JSON格式,PHP

返回JSON格式,PHP

问题描述:

In a project, I have to return user_id, user_age from the database and the return format should be like

  • user object which contains user_id and user_age
  • average age of users
  • count of users

the return format should be in JSON format. I have created user array and encoded to JSON by using the method

json_encode(user);

my code is like this :

 while ($row = mysql_fetch_array($result)) {

        $user["id"]                 = $row["id"];
        $user["name"]               = ucfirst($row["user_name"]);
        $user["date"]               = $row["date_of_treatment"];
        $user["age"]                = $row["age_of_user"];

        // push single user into final response array
        array_push($response, $user);

        $count = $count+1;
        $sum_of_age = $sum_of_age+$row["age_of_user"];

    }

echo json_encode($response);

I have calculated the average age ($sum_of_age/$count) and count of returned users ($count), but I don't know how to return average age and count of users with the same json response.any help will be appreciated.

在项目中,我必须从数据库返回user_id,user_age,返回格式应该像 p >

  • 用户对象 strong>,其中包含user_id和user_age li>
  • 平均用户年龄 li> \ n
  • 用户数 li> ul> blockquote>

    返回格式应为JSON格式。 我已创建用户数组并编码为 JSON strong>使用方法 p>

    json_encode(user); p> blockquote>

    我的代码 是这样的: p>

      while($ row = mysql_fetch_array($ result)){
     
     $ user [“id”] = $ row [“id”];  
     $ user [“name”] = ucfirst($ row [“user_name”]); 
     $ user [“date”] = $ row [“date_of_treatment”]; 
     $ user [“age”] = $  row [“age_of_user”]; 
     
     //将单个用户推送到最终响应数组
     array_push($ response,$ user); 
     
     $ count = $ cou  nt + 1; 
     $ sum_of_age = $ sum_of_age + $ row [“age_of_user”]; 
     
    } 
     
    echo json_encode($ response); 
      code>  pre> 
     
     

    我已经计算了平均年龄($ sum_of_age / $ count)和返回用户数($ count),但我不知道如何返回具有相同json响应的平均年龄和用户数。任何帮助将不胜感激 。 p> div>

You can do like this:

$count=0;
$sum_of_age=0;
$response=array();
$response['users']=array();


while ($row = mysql_fetch_array($result)) {

    $user["id"]                 = $row["id"];
    $user["name"]               = ucfirst($row["user_name"]);
    $user["date"]               = $row["date_of_treatment"];
    $user["age"]                = $row["age_of_user"];

    // push single user into final response array
    array_push($response['users'], $user);

    $count = $count+1;
    $sum_of_age = $sum_of_age+$row["age_of_user"];

}

$response['count']=$count;
$response['avg']=$sum_of_age/$count;
echo json_encode($response);

<?php

$response = array();

while ($row = mysql_fetch_array($result)) {

        $user["id"]                 = $row["id"];
        $user["name"]               = ucfirst($row["user_name"]);
        $user["date"]               = $row["date_of_treatment"];
        $user["age"]                = $row["age_of_user"];

        // push single user into final response array
        array_push($response, $user);

        $count = $count+1;
        $sum_of_age = $sum_of_age+$row["age_of_user"];

    }

$response["average_age"]  = $sum_of_age / $count;
$response["count"] = $count;

echo json_encode($response);

}

This is the answer, thanks @Amal Murali (commented a link), the link you have provided is working.. :-)

You can try this:

$users = array();
$sum_of_age = 0;
$count = 0;
$users = array();
while ($row = mysql_fetch_array($result)) 
{
    $user["id"] = $row["id"];
    $user["name"] = ucfirst($row["user_name"]);
    $user["date"] = $row["date_of_treatment"];
    $user["age"] = $row["age_of_user"];

    // push single user into final response array
    $users[] = $user;

    $count++;
    $sum_of_age += (int) $row["age_of_user"];
}

$response = array(
    'users' => $users,
    'averageAge' => $sum_of_age/$count,
    'count' => $count

);

echo json_encode($response);

This should result in the following json response:

{
    "users":[
        { "id" : "1", "name" : "John Doe" , "date" : "2014-03-22 15:20" , "age" : 42 },
        {...},
        ...
    ],
    "averageAge": 42,
    "count": 1337
}