日期:2014-05-17  浏览次数:20608 次

php循环求助

sql查询结果为
year mon
2012 5
2012 4
2012 3
2011 12
2011 11
...

怎么写php循环让它这样输出
2012 
5
4
3
2011
12
11
10
...

------解决方案--------------------
$array=array();

foreach(){
if(isset($array['year'])){
$array['year'][]=$result['mon'];
}else{
$array['year']=$result['year'];
$array['year'][]=$result['mon'];
}

foreach $array print_r;

大概这意思...
------解决方案--------------------
$last = '';
while($r = mysql_fetch_assoc($rs)) {
if($r['year'] != $last) {
echo $r['year'] . '<br>';
$last = $r['year'];
}
echo $r['mon'] . '<br>';
}
------解决方案--------------------
探讨

$last = '';
while($r = mysql_fetch_assoc($rs)) {
if($r['year'] != $last) {
echo $r['year'] . '<br>';
$last = $r['year'];
}
echo $r['mon'] . '<br>';
}