gpt4 book ai didi

php - 如何合并多个表数据并将json编码为一个数组?

转载 作者:行者123 更新时间:2023-11-28 23:38:49 25 4
gpt4 key购买 nike

我已经从一个月的表数据中编码了 json 数组,如下所示,但是如何在合并所有数据后将多个数据表与其他月份合并到一个 json 数组中并按部门分组?

<?php

include("dbconfig.php");

$sql = "SELECT dept, SUM(ttlot) AS sum_ot, SUM(ttlnorm) AS sum_norm
FROM month_jan
GROUP BY dept
ORDER BY sum_ot DESC";

$result = mysqli_query($dbconfig, $sql) or die("Error in Selecting " . mysqli_error($dbconfig));

$category = array();
$category['name'] = 'Dept';

$series1 = array();
$series1['name'] = 'Normal';

$series2 = array();
$series2['name'] = 'OT';

$emparray = array();
while ($row = mysqli_fetch_assoc($result)) {

$category['data'][] = $row['dept'];
$series1['data'][] = $row['sum_norm'];
$series2['data'][] = $row['sum_ot'];

}

$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);

$json = json_encode($result,JSON_NUMERIC_CHECK);

echo $json;

mysqli_close($dbconfig);
?>

一月份的输出:

[{"name":"Dept","data":["CNC","MACH","ANOD","BUFF","CAST","POLISH","SL","EPT","TUMB","TOOL","SHOT","QC","LOG","MAIN","LC","WWT","OG","NPD","E-COAT","SFT"]},{"name":"Normal","data":[47429.1,39975.7,34553.8,49075.9,28316.3,21237.1,13492.5,5848.2,7691.1,6963.9,5636.1,7555.8,5821.9,2161.2,1812,1191.7,1479.1,1299.6,11542.6,602]},{"name":"OT","data":[20041,17874,14431,13535.5,8800.5,5613.5,3569.5,3101,2327,2278,2237,2142,1810,942,690,456,297,110.5,66,50.5]}]

合并四个月后我想要什么结果:

[{"name":"Month","data":["Jan","Feb","Mac","Apr"]},{"name":"Normal","data":[504291,409757,295538,430759]},{"name":"OT","data":[89041,96874,81431,80535]}]

谁能帮我解决这个问题?

最佳答案

我是通过打印它们手动完成的,如果您对两个表的 JSON 编码有一些问题,我认为这是一个替代解决方案。有时具有相同名称的行会在 json_encode() 函数上产生错误。

<?php
session_start();

require("../config.php");
$output = array();

$id = $_GET['id'];

if(isset($_SESSION['user'])){
$f_data ='';
$sql = "SELECT * FROM quiz WHERE subject_id=$id ORDER BY id DESC";
$query=$conn->query($sql);
while($row=$query->fetch_array()){
$sql2 = "SELECT * FROM q_details WHERE id=$row[2] ORDER BY id DESC LIMIT 1";
$query2=$conn->query($sql2);
while($row2=$query2->fetch_array()){
$f_data .= '{
"id":"'.$row[0].'",
"subject_id":"'.$row[1].'",
"questionaire_id":"'.$row[2].'",
"name":"'.$row[3].'",
"description":"'.$row[4].'",
"start":"'.$row[5].'",
"end":"'.$row[6].'",
"date":"'.$row[7].'",
"questionaire_name":"'.$row2[2].'",
"questionaire_description":"'.$row2[3].'"
},';
}
}
$f_data = substr($f_data, 0, -1);
echo '['.$f_data.']';
}
else{
echo"<script>window.open('http://localhost/lnhs_app/#/','_self')</script>";
}

?>

如果您有同名的行,您应该重命名其他行,就像我在上面的示例中所做的那样 (questionaire_name, questionaire_description)

它将产生这样的输出:

[{ "id":"1", "subject_id":"2", "questionaire_id":"1", "name":"Quiz 1", "description":"Answer this quiz within 1 hour", "start":"7:30AM", "end":"8:30AM", "date":"08-01-18", "questionaire_name":"Right triangle", "questionaire_description":"Questionaire # 1" }]

关于php - 如何合并多个表数据并将json编码为一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com