Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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 while循环_Php_Arrays_Json_While Loop - Fatal编程技术网

如何在不重复重复数据的情况下正确设置PHP while循环

如何在不重复重复数据的情况下正确设置PHP while循环,php,arrays,json,while-loop,Php,Arrays,Json,While Loop,我看过无数以前的问题,找不到一个与我现在的问题相同的问题。我有一个工作脚本,可以从数据库中获取所需的数据 <?php $result = $conn->query("SELECT COUNT(colUser) AS userCount, colMonth as userMonth, colYear as userYear FROM MyTable GROUP BY userYear"); $newData = array(); $years = array(); $cnt = arr

我看过无数以前的问题,找不到一个与我现在的问题相同的问题。我有一个工作脚本,可以从数据库中获取所需的数据

<?php
$result = $conn->query("SELECT COUNT(colUser) AS userCount, colMonth as userMonth, colYear as userYear FROM MyTable GROUP BY userYear");
$newData = array();
$years = array();
$cnt = array();
while($selected_row = $result->fetch(PDO::FETCH_ASSOC)) {
    array_push($years,$selected_row['userMonth']);
    $tmp_key = $selected_row['userYear'];

    array_push($cnt,$selected_row['userCount']);
    $newData[$tmp_key] = array(
       implode(',',$cnt)
    );
}
$jsonResult = json_encode($newData);


echo $jsonResult;
我得到的结果是

{
  "2018": ["292,181,1868,1074,2726,2213,2616,3269,1744,2839,1983,1689"],
  "2019": ["292,181,1868,1074,2726,2213,2616,3269,1744,2839,1983,1689,2179,2027,1948,2071,2323,1963,2721,4065,4626,4660,3783,3170"],
  "2020": ["292,181,1868,1074,2726,2213,2616,3269,1744,2839,1983,1689,2179,2027,1948,2071,2323,1963,2721,4065,4626,4660,3783,3170,4308,3307,6143,6795,1881"]
}
此外,如果可能的话,更理想的产出是(用零填充空月份)


我真诚地感谢你的帮助

在格鲁比克劳顿公司的指导下,我成功地实现了我想要的结果

$result = $conn->query("SELECT COUNT(colUser) AS userCount, colMonth as userMonth, colYear as userYear FROM MyTable GROUP BY userYear");
$newData = array();
$years = array();
$cnt = array();
while($selected_row = $result->fetch(PDO::FETCH_ASSOC)) {
    array_push($years,$selected_row['userMonth']);
    $tmp_key = $selected_row['userYear'];
    $newData[$tmp_key][] = $selected_row['userCount'];
}
$jsonResult = json_encode($newData);


echo $jsonResult;

为什么要将值作为单个字符串存储在数组中?换句话说,您可以只做
$newData[$tmp_key]=$cnt
,而不是
内爆(','$cnt)
——如果您不想重复值,则需要保留已使用值的列表,并确保不再添加这些值。如果我们能在while循环之前看到您的数据是如何格式化的,这对我们也会有很大帮助。我不会说我是数组新手,但还不是完全的中级。因此,如果我做了一些不正确的事情,我是不知道的。while循环之前的数据只是从SQL查询返回的数据。没什么特别的。数据库只有年和月列,然后是用户ID。我知道我可以通过第二次查询来完成我想要的,但我试图避免这样做。
{
  "2018": ["292,181,1868,1074,2726,2213,2616,3269,1744,2839,1983,1689"],
  "2019": ["2179,2027,1948,2071,2323,1963,2721,4065,4626,4660,3783,3170"],
  "2020": ["4308,3307,6143,6795,1881,0,0,0,0,0,0,0"]
}
$result = $conn->query("SELECT COUNT(colUser) AS userCount, colMonth as userMonth, colYear as userYear FROM MyTable GROUP BY userYear");
$newData = array();
$years = array();
$cnt = array();
while($selected_row = $result->fetch(PDO::FETCH_ASSOC)) {
    array_push($years,$selected_row['userMonth']);
    $tmp_key = $selected_row['userYear'];
    $newData[$tmp_key][] = $selected_row['userCount'];
}
$jsonResult = json_encode($newData);


echo $jsonResult;