我想查看一个名为$rows
的数组。对于ifDesc
中的每个不同系列,如果 all Octets
是0
,则将其删除,但是如果单个条目> 0,则保留整个系列,包括代表不同的ifDesc
我尝试使用PHP或JavaScript(作为ajax响应传递)通过foreach遍历此问题,但是在使用独特的ifDesc
这是为便于阅读而编码的json数组。
0: {id: "40", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 14:45:10"}
1: {id: "87", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 15:00:10"}
2: {id: "134", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 15:15:09"}
3: {id: "181", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 15:30:09"}
4: {id: "228", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 15:45:09"}
5: {id: "275", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 16:00:09"}
6: {id: "322", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 16:15:09"}
7: {id: "369", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 16:30:09"}
8: {id: "416", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 16:45:09"}
9: {id: "463", ifDesc: "imq0 - In", Octets: 0, date: "2019-07-12 17:00:11"}
10: {id: "36", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 14:45:09"}
11: {id: "83", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 15:00:09"}
12: {id: "130", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 15:15:08"}
13: {id: "177", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 15:30:08"}
14: {id: "224", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 15:45:08"}
15: {id: "271", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 16:00:09"}
16: {id: "318", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 16:15:08"}
17: {id: "365", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 16:30:08"}
18: {id: "412", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 16:45:08"}
19: {id: "459", ifDesc: "lo - In", Octets: 0, date: "2019-07-12 17:00:10"}
20: {id: "37", ifDesc: "Port1 - In", Octets: 0, date: "2019-07-12 14:45:09"}
21: {id: "84", ifDesc: "Port1 - In", Octets: 0.021765, date: "2019-07-12 15:00:09"}
22: {id: "131", ifDesc: "Port1 - In", Octets: 0.029837, date: "2019-07-12 15:15:08"}
23: {id: "178", ifDesc: "Port1 - In", Octets: 0.055927, date: "2019-07-12 15:30:08"}
24: {id: "225", ifDesc: "Port1 - In", Octets: 0.043918, date: "2019-07-12 15:45:08"}
25: {id: "272", ifDesc: "Port1 - In", Octets: 0.029381, date: "2019-07-12 16:00:09"}
26: {id: "319", ifDesc: "Port1 - In", Octets: 0.021934, date: "2019-07-12 16:15:09"}
27: {id: "366", ifDesc: "Port1 - In", Octets: 0.023848, date: "2019-07-12 16:30:09"}
28: {id: "413", ifDesc: "Port1 - In", Octets: 0.001438, date: "2019-07-12 16:45:08"}
29: {id: "460", ifDesc: "Port1 - In", Octets: 0.000928, date: "2019-07-12 17:00:10"}
30: {id: "42", ifDesc: "Port1.100 - In", Octets: 0, date: "2019-07-12 14:45:10"}
31: {id: "89", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 15:00:11"}
32: {id: "136", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 15:15:09"}
33: {id: "183", ifDesc: "Port1.100 - In", Octets: 0.000003, date: "2019-07-12 15:30:09"}
34: {id: "230", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 15:45:09"}
35: {id: "277", ifDesc: "Port1.100 - In", Octets: 0.000002, date: "2019-07-12 16:00:10"}
36: {id: "324", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 16:15:10"}
37: {id: "371", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 16:30:10"}
38: {id: "418", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 16:45:09"}
39: {id: "465", ifDesc: "Port1.100 - In", Octets: 0.000001, date: "2019-07-12 17:00:11"}
40: {id: "41", ifDesc: "Port1.2 - In", Octets: 0, date: "2019-07-12 14:45:10"}
41: {id: "88", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 15:00:10"}
42: {id: "135", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 15:15:09"}
43: {id: "182", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 15:30:09"}
44: {id: "229", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 15:45:09"}
45: {id: "276", ifDesc: "Port1.2 - In", Octets: 0.000038, date: "2019-07-12 16:00:10"}
46: {id: "323", ifDesc: "Port1.2 - In", Octets: 0.000034, date: "2019-07-12 16:15:09"}
47: {id: "370", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 16:30:09"}
48: {id: "417", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 16:45:09"}
49: {id: "464", ifDesc: "Port1.2 - In", Octets: 0.000033, date: "2019-07-12 17:00:11"}
50: {id: "38", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 14:45:09"}
51: {id: "85", ifDesc: "Port2 - In", Octets: 0.125069, date: "2019-07-12 15:00:10"}
52: {id: "132", ifDesc: "Port2 - In", Octets: 0.187321, date: "2019-07-12 15:15:08"}
53: {id: "179", ifDesc: "Port2 - In", Octets: 0.13348, date: "2019-07-12 15:30:08"}
54: {id: "226", ifDesc: "Port2 - In", Octets: 0.284196, date: "2019-07-12 15:45:09"}
55: {id: "273", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 16:00:09"}
56: {id: "320", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 16:15:09"}
57: {id: "367", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 16:30:09"}
58: {id: "414", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 16:45:09"}
59: {id: "461", ifDesc: "Port2 - In", Octets: 0, date: "2019-07-12 17:00:10"}
60: {id: "39", ifDesc: "Port8 - In", Octets: 0, date: "2019-07-12 14:45:10"}
61: {id: "86", ifDesc: "Port8 - In", Octets: 0.000056, date: "2019-07-12 15:00:10"}
62: {id: "133", ifDesc: "Port8 - In", Octets: 0.000299, date: "2019-07-12 15:15:09"}
63: {id: "180", ifDesc: "Port8 - In", Octets: 0.00072, date: "2019-07-12 15:30:09"}
64: {id: "227", ifDesc: "Port8 - In", Octets: 0.003257, date: "2019-07-12 15:45:09"}
65: {id: "274", ifDesc: "Port8 - In", Octets: 0.000062, date: "2019-07-12 16:00:09"}
66: {id: "321", ifDesc: "Port8 - In", Octets: 0.000042, date: "2019-07-12 16:15:09"}
67: {id: "368", ifDesc: "Port8 - In", Octets: 0.000074, date: "2019-07-12 16:30:09"}
68: {id: "415", ifDesc: "Port8 - In", Octets: 0.000037, date: "2019-07-12 16:45:09"}
69: {id: "462", ifDesc: "Port8 - In", Octets: 0.000063, date: "2019-07-12 17:00:10"}
70: {id: "43", ifDesc: "reds2 - In", Octets: 0, date: "2019-07-12 14:45:10"}
71: {id: "90", ifDesc: "reds2 - In", Octets: 0.119396, date: "2019-07-12 15:00:11"}
72: {id: "137", ifDesc: "reds2 - In", Octets: 0.178453, date: "2019-07-12 15:15:10"}
73: {id: "184", ifDesc: "reds2 - In", Octets: 0.126522, date: "2019-07-12 15:30:10"}
74: {id: "231", ifDesc: "reds2 - In", Octets: 0.356846, date: "2019-07-12 15:45:10"}
75: {id: "278", ifDesc: "reds2 - In", Octets: 0.156991, date: "2019-07-12 16:00:10"}
76: {id: "325", ifDesc: "reds2 - In", Octets: 0.04118, date: "2019-07-12 16:15:10"}
77: {id: "372", ifDesc: "reds2 - In", Octets: 0.143545, date: "2019-07-12 16:30:10"}
78: {id: "419", ifDesc: "reds2 - In", Octets: 0.014859, date: "2019-07-12 16:45:10"}
79: {id: "466", ifDesc: "reds2 - In", Octets: 0.010649, date: "2019-07-12 17:00:11"}
80: {id: "40", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 14:45:10"}
81: {id: "87", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 15:00:10"}
82: {id: "134", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 15:15:09"}
83: {id: "181", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 15:30:09"}
84: {id: "228", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 15:45:09"}
85: {id: "275", ifDesc: "imq0 - Out", Octets: 0.000061, date: "2019-07-12 16:00:09"}
86: {id: "322", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 16:15:09"}
87: {id: "369", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 16:30:09"}
88: {id: "416", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 16:45:09"}
89: {id: "463", ifDesc: "imq0 - Out", Octets: 0, date: "2019-07-12 17:00:11"}
90: {id: "36", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 14:45:09"}
91: {id: "83", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 15:00:09"}
92: {id: "130", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 15:15:08"}
93: {id: "177", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 15:30:08"}
94: {id: "224", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 15:45:08"}
95: {id: "271", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 16:00:09"}
96: {id: "318", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 16:15:08"}
97: {id: "365", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 16:30:08"}
98: {id: "412", ifDesc: "lo - Out", Octets: 0, date: "2019-07-12 16:45:08"}
当此数据传递到图表时,我希望消除在图表底部所有值均为0的一系列线。
在上面的数组中,我希望删除imq0 - In
= 0的总和。lo - In
,lo - Out
和Octets
。
由于存储的数据不是您所看到的,所以要复杂一些,构建数组之前需要进行一些计算,如下所示:
$SQLIn = "SELECT id, CONCAT(ifDesc, \" - In\") as ifDesc, ifInOctets AS Octets, date FROM table WHERE applianceKey = 'XXXX' AND date between (CURTIME() - INTERVAL 3 DAY ) and CURTIME() ORDER BY ifDesc, date";
$SQLOut = "SELECT id, CONCAT(ifDesc, \" - Out\") as ifDesc, ifOutOctets AS Octets, date FROM table WHERE applianceKey = 'XXXX' AND date between (CURTIME() - INTERVAL 3 DAY ) and CURTIME() ORDER BY ifDesc, date";
$ResultsIn = mysqli_query($dbST, $SQLIn) or die();
$ResultsOut = mysqli_query($dbSTS, $SQLOut) or die();
$SQLRowsIn = mysqli_num_rows($ResultsIn);
$SQLRowsOut = mysqli_num_rows($ResultsOut);
$curOct = 0; $prevOct = 0; $curIf = ''; $prevIf = '';
$rows = array();
if ($SQLRowsIn > 0) {
while ($rowIn = MySQLi_fetch_assoc($ResultsIn)) {
$curIf = $rowIn['ifDesc'];
$curOct = $rowIn['Octets'];
if ($curIf === $prevIf) { $clc = $curOct-$prevOct; }
else { $clc = $curOct-$curOct; }
if ($clc < 0) { $clc = $curOct-$prevOct+4294967295; }
$B = $clc*8;
$kB = $B/1000;
$mB = $kB/1000;
$mBps = $mB/900;
$rowIn['Octets'] = $mBps;
$rows[] = $rowIn;
$prevIf = $curIf;
$prevOct = $curOct;
}
$curOct = 0; $prevOct = 0; $curIf = ''; $prevIf = '';
while ($rowOut = MySQLi_fetch_assoc($ResultsOut)) {
$curIf = $rowOut['ifDesc'];
$curOct = $rowOut['Octets'];
if ($curIf === $prevIf) { $clc = $curOct-$prevOct; }
else { $clc = $curOct-$curOct; }
if ($clc < 0) { $clc = $curOct-$prevOct+4294967295; }
$B = $clc*8;
$kB = $B/1000;
$mB = $kB/1000;
$mBps = $mB/900;
$rowOut['Octets'] = $mBps;
$rows[] = $rowOut;
$prevIf = $curIf;
$prevOct = $curOct;
}
echo json_encode($rows);
答案 0 :(得分:3)
更改查询以将其过滤掉。
SELECT id, CONCAT(t1.ifDesc, " - In") as ifDesc, ifInOctets AS Octets, date
FROM table AS t1
JOIN (
SELECT DISTINCT ifDesc
FROM table
WHERE applianceKey = 'XXXX' AND date between (CURTIME() - INTERVAL 3 DAY ) and CURTIME()
AND ifInOctets != 0
) AS t2 ON t1.ifDesc = t2.ifDesc
WHERE applianceKey = 'XXXX' AND date between (CURTIME() - INTERVAL 3 DAY ) and CURTIME()
ORDER BY ifDesc, date
子查询仅返回至少具有一个非零行的ifDesc
。
答案 1 :(得分:2)
我要做的一种方法是首先获得所有ifDesc
。收集所有em后,就可以开始处理实际数组以删除总和为零的ifDesc
批。
$ifDescs = array_unique(array_column($rows, 'ifDesc')); // get all ifDescs
foreach ($ifDescs as $currentIfDesc) { // loop each ifDesc batches
$current_batch = []; // set initial batch of ifDesc type for temporary container
foreach ($rows as $k => $row) { // group them first
if ($row['ifDesc'] === $currentIfDesc) {
$current_batch[$k] = $row;
}
}
$non_zero_octets = array_sum(array_column($current_batch, 'Octets')) > 0; // get all octets of the current batch iteration, and check if its greater than one when summed
if (!$non_zero_octets) { // if not greater than zero
$rows = array_diff_key($rows, array_keys($current_batch)); // remove em using all the keys
}
}
但是值得。我同意Barmar使用查询的解决方案,以使其扩展性更好。
答案 2 :(得分:0)
借助Ghost的答案,我们计算了每个不同的ifDesc
的总和,如果> 0,则将其添加到新数组中。
$ifDescs = array_unique(array_column($rows, 'ifDesc'));
$recalculatedRows = [];
foreach ($ifDescs as $currentIfDesc) {
$current_batch = [];
foreach ($rows as $k => $row) {
if ($row['ifDesc'] === $currentIfDesc) {
$current_batch[$k] = $row;
}
}
if (array_sum(array_column($current_batch, 'Octets')) > 0) { //sum of Octets > 0 ?
foreach ($current_batch as $r) { //if yes, loop through $current_batch
$recalculatedRows[] = $r; //adding series to new array
}
}
}
echo json_encode($recalculatedRows);