我无法弄清楚如何过滤/排序这些数组,它显示第1部分,然后是第1部分的所有子部分(1,2,3,...),而不重复第1部分。
接下来,它将循环到第2节,然后循环第2节的所有子节(1,2,3,...),而不重复第2节。
必须有一个if语句或某些东西来帮助阻止这些部分显示每个子部分。
<?php
echo "<div class='panel panel-default'>";
$numrows = count($tabs);
for ($i =0; $i < $numrows; $i++){
$sectionnumrows = count($tabs[$i]['section']);
echo $tabs[$i]['section'].","; // don't want this to repeat, only show once
for ($j=0; $j < $sectionnumrows; $j++){
$subsectionnumrows = count($tabs[$i]['subsection']);
for ($k=0; $k < $sectionnumrows; $k++){
if([$i]['section'] == $j){
echo "subsection ".$tabs[$i]['subsection']."<br>";
}
}
}
}
echo "</div>";
输出部分,= 1
然后只显示第1部分= 1,2,3
的小节然后输出部分,= 2
然后只显示第2部分= 1,2,3,4
的小节数组输出的样本
import csv
with open('/home/user/python/data.csv') as csvfile :
unique_values = {}
for row in csv.reader(csvfile, delimiter=','):
element = row[0]
if element not in unique_values:
unique_values[element] = 0
unique_values[element] += 1
for unique, count in unique_values.items():
print('{0},{1}'.format(unique, count))
我正在尝试这个循环,但无法弄清楚如何阻止该部分重复:
numpy.where
答案 0 :(得分:1)
只需更改您的查询,您将只获得唯一的部分,并且如果我理解您的需要,所有子部分将会合并。
SELECT section, GROUP_CONCAT(subsection) as subsection
FROM table
WHERE id = :id
GROUP BY section
ORDER BY section, subsection
答案 1 :(得分:1)
基于Mihai回答:
$numrows = count($tabs);
for ($i =0; $i < $numrows; $i++){
echo "section" . $tabs[$i]['section'].",";
$result = explode(',', $tabs[$i]['subsection']); // $row['subsection'] contains string 1,2,3.. .etc
foreach($result as $row){
echo "<br> subsection ".$row ;
}
}
如果你有2个GROUP_CONCAT小节,这是一个例子:
SELECT section,
GROUP_CONCAT(subsection) as subsection1,
GROUP_CONCAT(subsection2) as subsection2
FROM table
WHERE id = :id
GROUP BY section
ORDER BY section, subsection
$combined = array_combine($subsection1 , $subsection2);
foreach ($combined as $subsection1 => $subsection2){
echo "$subsection1 => $subsection2";
}