[英]PHP Custom json_encode Formatting With A Join From MySQL
我正在創建一個項目時間跟蹤工具。 我是JOINS的菜鳥。 我對非關系型數據庫有更多的經驗。
我有一個帶有兩個表的MySQL數據庫。 一個用於列表或項目,稱為“ Projects”,另一個用於保存每個會話,稱為“ ProjectLogs”。 每個會話都有一個開始和停止時間戳記。
“項目”表如下所示:
p_id, projectname
“ ProjectLogs”如下所示:
id, project_id, starttime, endtime
我正在使用PHP進行左加入,使用此方法:
$sql = "SELECT *
FROM Projects
LEFT JOIN ProjectLogs
ON Projects.p_id=ProjectLogs.project_id";
然后我得到結果:
$result = mysqli_query($con, $sql);
然后,我需要將$ result轉換為JSON,因此我使用以下代碼:
$emparray = array();
while($row = mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}
header('Content-Type: application/json');
echo json_encode($emparray);
我回來的是這個。 每個TimeLog都有一個不同的對象:
[
{
"p_id":"1",
"projectname":"Project 001",
"id":"1",
"project_id":"1",
"starttime":"2015-08-09 19:37:02",
"endtime":"2015-08-09 19:39:13"
}
]
我想要的是這樣的東西,其中日志是項目內部的一個數組:
[
{
"p_id": "1",
"projectname": "Project 001"
"ProjectLogs": [
{
"id": "1",
"project_id": "1",
"starttime": "2015-08-09 19:44:24",
"endtime": "2015-08-09 20:00:17"
},
{
"id": "2",
"project_id": "1",
"starttime": "2015-08-09 19:44:24",
"endtime": "2015-08-09 20:00:17"
}
]
},
{
"p_id": "2",
"projectname": "Project 002"
"ProjectLogs": [
{
"id": "1",
"project_id": "2",
"starttime": "2015-08-09 19:44:24",
"endtime": "2015-08-09 20:00:17"
},
{
"id": "2",
"project_id": "2",
"starttime": "2015-08-09 19:44:24",
"endtime": "2015-08-09 20:00:17"
}
]
}
]
任何幫助將不勝感激! 我覺得這是一個重復的問題,但是我不確定要搜索什么。
我想出了答案。 如果有人感興趣,這是解決方案:
$sql = "SELECT *
FROM Projects
LEFT JOIN ProjectLogs
ON Projects.p_id=ProjectLogs.project_id
ORDER BY Projects.p_id";
$result = mysqli_query($con, $sql);
// create an empty array that you can send
// to json_encode later
$arr = array();
// Loop through the results
while($row = mysqli_fetch_assoc($result)) {
// Check to see if the result contains a 'project_id' key
// If it doesn't, then this isn't a result with a ProjectLog
if( $row['project_id'] != "" ) {
// Set the key vaue pairs for the Project
$arr[$row['p_id']]['p_id'] = $row['p_id'];
$arr[$row['p_id']]['projectname'] = $row['projectname'];
// Now we need to check to see if any timelogs have been
// pushed to $arr.
if(array_key_exists("timelogs", $arr[$row['p_id']]) ) {
// Since it does exist:
// Create a variable to store the 3 values we need,
// And push them to the existing timelogs array
$val = array(
'id' => $row['id'],
'starttime' => $row['starttime'],
'endtime' => $row['endtime']
);
array_push($arr[$row['p_id']]['timelogs'], $val);
} else {
// Since it doesn't exist, lets create the timelogs array
$arr[$row['p_id']]['timelogs'] = array(
array(
'id' => $row['id'],
'starttime' => $row['starttime'],
'endtime' => $row['endtime']
)
);
}
}else {
// This fires when there are no ProjectLogs yet, just an empty project
$arr[$row['p_id']]['p_id'] = $row['p_id'];
$arr[$row['p_id']]['projectname'] = $row['projectname'];
}
}
// Apparently this is required
header('Content-Type: application/json');
// Go time! JSON!
echo json_encode($arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.