Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP MYSQL循环JSON创建,不使用不同的查询_Php_Mysql_Json - Fatal编程技术网

通过PHP MYSQL循环JSON创建,不使用不同的查询

通过PHP MYSQL循环JSON创建,不使用不同的查询,php,mysql,json,Php,Mysql,Json,您好,我将如何循环在PHP MYSQL中创建的Json,这样我就不需要声明单个查询来生成所需的输出 我希望创建一个JSON,而不需要单独声明变量和查询来生成单个数组 <?php include("connection.php"); $sth = mysqli_query($connect,"SELECT month as month FROM data group by month ORDER BY month DESC"); $rows = array(); $rows['name']

您好,我将如何循环在PHP MYSQL中创建的Json,这样我就不需要声明单个查询来生成所需的输出

我希望创建一个JSON,而不需要单独声明变量和查询来生成单个数组

<?php
include("connection.php");

$sth = mysqli_query($connect,"SELECT month as month FROM data group by month ORDER BY month DESC");
$rows = array();
$rows['name'] = 'Month';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['month'];
}

$sth = mysqli_query($connect,"SELECT sum(cf) as cf FROM  data Where branch='NYC'  group by month");


$rows1 = array();
$rows1['name'] = 'NYC';
while($r = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $r['cf'];
}


$sth = mysqli_query($connect,"SELECT sum(cf) as cf FROM data Where branch='LA' group by month");
$rows2 = array();
$rows2['name'] = 'LA';
while($rr = mysqli_fetch_assoc($sth) ) {
$rows2['data'][] = $rr['cf'];
}




$result = array();

array_push($result,$rows);
array_push($result,$rows1);
array_push($result,$rows2);

echo(json_encode($result, JSON_NUMERIC_CHECK)) ;


?>

我想这会满足你的要求。请注意,我已更改了您的ORDER BY条款,以便它能够正确订购一年中的月份

$sth = mysqli_query($conn, 'SELECT month, branch, sum(cf) AS cf 
                            FROM data 
                            GROUP BY month, branch
                            ORDER BY MONTH(STR_TO_DATE(month, "%M"))');
$months = array();
$branches = array();
while ($row = mysqli_fetch_assoc($sth)) {
    $month = $row['month'];
    $branch = $row['branch'];
    if (!in_array($month, $months)) $months[] = $month;
    if (!in_array($branch, array_keys($branches))) $branches[$branch] = array();
    $branches[$branch][$month] = $row['cf'];
}
$out = array();
$out[] = (object) [ 'name' => "Month", 'data' => $months ];
foreach ($branches as $branch => $data) {
    $out[] = (object) [ 'name' => $branch, 'data' => array_values($data)];
}
echo json_encode($out, JSON_NUMERIC_CHECK);

你忘了告诉我们你的问题是什么!什么是“我需要的输出”?嗨,Jeff,我需要的输出与代码的输出相同,我只需要减少我正在执行的查询,因为我的查询中的分支数不仅是2个,而且可能会增加超时时间。
$sth = mysqli_query($conn, 'SELECT month, branch, sum(cf) AS cf 
                            FROM data 
                            GROUP BY month, branch
                            ORDER BY MONTH(STR_TO_DATE(month, "%M"))');
$months = array();
$branches = array();
while ($row = mysqli_fetch_assoc($sth)) {
    $month = $row['month'];
    $branch = $row['branch'];
    if (!in_array($month, $months)) $months[] = $month;
    if (!in_array($branch, array_keys($branches))) $branches[$branch] = array();
    $branches[$branch][$month] = $row['cf'];
}
$out = array();
$out[] = (object) [ 'name' => "Month", 'data' => $months ];
foreach ($branches as $branch => $data) {
    $out[] = (object) [ 'name' => $branch, 'data' => array_values($data)];
}
echo json_encode($out, JSON_NUMERIC_CHECK);