问题描述
当试图从foreach()中的数组中使用json_encode时,数据会移动到下一个值中,例如:
我有这张来自同样的查询+ php:
当使用json_encode试图将这些表格数据填入highcharts时,我的结果如下所示:
[{ 名称: 用户1, 数据:[0,2,0,0]},{ 名称: 用户2, 数据:[0,2,0 ,0,2,0,2,4]}]
因此,它将User1的数据移动到User2的
我想要的结果是:
$ p $ [{name :User1,data:[0,2,0,0]},{name:User2,data:[2,0,2,4]}]
这是我的代码:
$ uniqueHours = array();
$ series = array();
$ data = array();
$ b foreach($ pdo-> query(SELECT DATEPART(HOUR,AL.EndDateTime)AS'EndHour'
FROM ActivityLog AL
WHERE
。$
ORDER BY AL.EndDateTime,'EndHour'DESC)作为$ row)
{
if(in_array($ row ['EndHour'],$ uniqueHours )){
continue;
}
$ uniqueHours [] = $ row ['EndHour'];
}
$ ODsql =SELECT * FROM Users;
foreach($ pdo-> query($ ODsql)as $ row)
{
echo'< tr>';
$ SCardNumber = $ row ['CardNumber'];
$ SEmployeeName = $ row ['用户名'];
echo'< td>'。$ SEmployeeName。'< / td>';
$ chartValues =;
foreach($ uniqueHours as $ hour)
{
$ countSQL = $ pdo-> prepare(SELECT DISTINCT(COUNT(AL.TerminalNumber)) AS TOTALS
FROM ActivityLog AL,WavePick WP
WHERE AL.TransactionType ='SPK'AND WP.PickedQty> 0
AND DATEPART(HOUR,AL.EndDateTime)=。$ hour。
AND AL.StartDateTime> ='。$ StartDate。00:00:00.000'AND AL.EndDateTime< ='。$ StartDate。23:59:59.000'
AND AL.OrderNumber = WP.OrderNumber
AND AL.CardNumber ='。$ SCardNumber。'
);
$ countSQL-> execute();
$ result = $ countSQL-> fetch(PDO :: FETCH_OBJ);
$ row_count = $ result-> TOTALS;
$ totals [] = $ result-> TOTALS;
echo'< td>'。$ row_count。'< / td>';
}
echo'< / tr>';
$ series ['name'] = $ SEmployeeName;
$ series ['data'] = $ totals;
array_push($ data,$ series);
}
我还没有把这个放到图表中因为数据无效。
我使用它来返回结果:
echo< div class ='well'> ;
打印json_encode($ results,JSON_NUMERIC_CHECK);
回显< / div>;
我怎样才能让这个数据只显示给它链接的每个用户?
解决方案
在此循环之前:
foreach $ unique $ as $ hour
$ b $ p $ empty $ $
$ total = array();
foreach($ uniqueHours as $ hour)
When trying to use json_encode from arrays in foreach() the data moves in to the next values, for example:
I have this table which is coming from the same query + php:
when using json_encode to try and fit this tables data in to highcharts, my outcome is like this:
[{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[0,2,0,0,2,0,2,4]}]
So it's moving User1's data in to User2's
My desired outcome would be:
[{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[2,0,2,4]}]
This is my code:
$uniqueHours = array();
$series = array();
$data = array();
foreach ($pdo->query("SELECT DATEPART(HOUR, AL.EndDateTime) AS 'EndHour'
FROM ActivityLog AL
WHERE
".$where."
ORDER BY AL.EndDateTime, 'EndHour' DESC") as $row)
{
if ( in_array($row['EndHour'], $uniqueHours) ) {
continue;
}
$uniqueHours[] = $row['EndHour'];
}
$ODsql = "SELECT * FROM Users";
foreach ($pdo->query($ODsql) as $row)
{
echo '<tr>';
$SCardNumber = $row['CardNumber'];
$SEmployeeName = $row['Username'];
echo '<td>'.$SEmployeeName.'</td>';
$chartValues = "";
foreach($uniqueHours as $hour)
{
$countSQL= $pdo->prepare("SELECT DISTINCT(COUNT(AL.TerminalNumber)) AS TOTALS
FROM ActivityLog AL, WavePick WP
WHERE AL.TransactionType = 'SPK' AND WP.PickedQty > 0
AND DATEPART(HOUR, AL.EndDateTime) = ".$hour."
AND AL.StartDateTime >= '".$StartDate." 00:00:00.000' AND AL.EndDateTime <= '".$StartDate." 23:59:59.000'
AND AL.OrderNumber = WP.OrderNumber
AND AL.CardNumber = '".$SCardNumber."'
");
$countSQL->execute();
$result = $countSQL->fetch(PDO::FETCH_OBJ);
$row_count = $result->TOTALS;
$totals[] = $result->TOTALS;
echo '<td>'.$row_count.'</td>';
}
echo '</tr>';
$series['name'] = $SEmployeeName;
$series['data'] = $totals;
array_push($data,$series);
}
I haven't actually put this in to the chart yet because the data is invalid.I am using this to return the outcome:
echo "<div class='well'>";
print json_encode($results, JSON_NUMERIC_CHECK);
echo "</div>";
How can I make this data show only for each user it is linked to?
解决方案 Before this loop:
foreach($uniqueHours as $hour)
empty $total array with
$total=array();
foreach($uniqueHours as $hour)
这篇关于json_encode返回下一行值 - PHP PDO SQL HighCharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!