所以我有以下两个sql查询,我正在尝试构建一个JSON对象以供我的Angular前端使用。
$getCompDetails = mysql_query("SELECT *
FROM competitions c
WHERE c.competitionId = '$competitionId'") or die("Couldn't select competition details");
$compDetailsArr = array();
while ($row = mysql_fetch_array($getCompDetails,MYSQL_ASSOC)){
array_push($compDetailsArr,$row);
}
$getCompUsers = mysql_query("SELECT c.userid, u.name, u.profilePic FROM competitionmembers c INNER JOIN users1 u ON u.id = c.userid WHERE competitionid = '$competitionId'") or die("Couldn't select competitions users");
$compUsersArr = array();
while ($row = mysql_fetch_array($getCompUsers,MYSQL_ASSOC)){
array_push($compUsersArr,$row);
}
$getUsrActs = mysql_query("SELECT activity_typeid, userid, time, activity_weight, activityname
FROM activity_entries
WHERE competitionid = '$competitionId'") or die("Couldn't select activities");
$userActArr = array();
while ($row = mysql_fetch_array($getUsrActs,MYSQL_ASSOC)){
array_push($userActArr,$row);
}
$compDetailsArr["users"] = $compUsersArr;
$compDetailsArr["users"]["activities"] = $userActArr;
echo json_encode($compDetailsArr);
结果如下:
{
"0": {
"id": "3371",
"organisationId": "524bd7c65fd60",
"competitionId": "52a99783c5d6f",
"name": "Nordic Challenge",
"start_date": "2013-12-12",
"end_date": "2014-05-24",
"createdby": "531",
"about": "asdasd",
"prize": "",
"prizeImage": "",
"compCreatedTime": "2013-12-12 11:01:25"
},
"users": {
"0": {
"userid": "531",
"name": "Jane Wilson",
"profilePic": "A4cMVn-yxQu4fLrpGU7_wwWfbFmAWlt40yS31gv1bTc.png"
},
"1": {
"userid": "1",
"name": "James Pember",
"profilePic": "fighter-1.jpg"
},
"2": {
"userid": "1061",
"name": "Olof Petersson",
"profilePic": ""
},
"3": {
"userid": "1071",
"name": "Lars Petersen",
"profilePic": ""
},
"4": {
"userid": "1081",
"name": "Mark Johnson",
"profilePic": ""
},
"5": {
"userid": "971",
"name": "676",
"profilePic": "784322701.jpg"
},
"activities": [
{
"activity_typeid": "12251",
"userid": "531",
"time": "2013-12-12 11:01:43",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12261",
"userid": "1061",
"time": "2013-12-12 11:05:44",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12271",
"userid": "1071",
"time": "2013-12-12 11:06:46",
"activity_weight": "50",
"activityname": "Deal"
},
{
"activity_typeid": "12251",
"userid": "1081",
"time": "2013-12-12 11:07:45",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12261",
"userid": "531",
"time": "2013-12-12 11:10:38",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12271",
"userid": "531",
"time": "2013-12-12 16:17:02",
"activity_weight": "50",
"activityname": "Deal"
},
{
"activity_typeid": "12271",
"userid": "1",
"time": "2013-12-27 10:17:30",
"activity_weight": "50",
"activityname": "Deal"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-08 17:12:30",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-08 18:41:03",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12271",
"userid": "971",
"time": "2014-01-08 18:43:50",
"activity_weight": "50",
"activityname": "Deal"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-08 18:44:10",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-08 18:50:02",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-08 18:50:40",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-08 18:58:02",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-09 13:02:25",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "971",
"time": "2014-01-09 13:03:16",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-10 09:40:59",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-10 09:41:03",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-24 13:00:56",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12261",
"userid": "1",
"time": "2014-01-24 13:01:01",
"activity_weight": "15",
"activityname": "Conducted"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-24 13:01:27",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-27 13:04:04",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-30 15:23:24",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-30 15:42:37",
"activity_weight": "10",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-30 19:31:27",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-01-30 19:31:44",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-02-07 14:25:32",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-02-10 12:41:16",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-02-10 12:41:51",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-02-24 15:01:33",
"activity_weight": "12",
"activityname": "Booked"
},
{
"activity_typeid": "12251",
"userid": "1",
"time": "2014-02-28 07:19:12",
"activity_weight": "12",
"activityname": "Booked"
}
]
}
}
这是所需的输出格式,每个用户都是单独的{}
{
"organisationId": 1,
"competitionId": "52eabcf0f3672",
"currentUser": 2,
"title": "Sales Hood Q1 Challenge",
"end_date": "2014-03-01 00:00:00",
"description": "This is it guys, challenge time!",
"prizeImage": "placeholder.jpg",
"prizeDescription": "Dinner for 2!",
"users": [{
"id": 2,
"name": "John Wilson",
"profilePic": "fighter-1.jpg",
"activities" = [{
"activity_typeid": 312,
"activityname": Calls
"activity_weight": 10
},
{
"activity_typeid": 312,
"activityname": Calls
"activity_weight": 10
}]
}, {
"id": 3,
"name": "Robin Christoper",
"profilePic": "fighter-3.jpg",
"activities" = [{
"activity_typeid": 312,
"activityname": Calls
"activity_weight": 10
}]
}, {
"id": 1,
"name": "Ronald Johnson",
"profilePic": "placeholder.jpg",
"activities" = [{
"activity_typeid": 312,
"activityname": Calls
"activity_weight": 10
}]
}]
}
愿意为此提供一些帮助!
答案 0 :(得分:3)
不要合并数组,而是将它们嵌套:
$compDetailsArr['users'] = $compUsersArr
$json = json_encode($compDetailsArr);
答案 1 :(得分:-1)
我有同样的情况试试这个:
$compDetailsArr = $compDetailsArr;
$compDetailsArr['users'] = array();
$compDetailsArr['users'] = $compUsersArr;
$json = json_encode($compDetailsArr);