add assigned ordered number to array entry php
Hey guys I have an assigned array from mysql results and I simply want to number them starting at one. Does anyone know ho开发者_高级运维w to do this?
while ($row=mysql_fetch_assoc($query)){
$out[] = array("ASSIGNED INTEGER", $row['total']);
}
You'll have to use a variable as a counter, to keep track of the line you're on :
$counter = 1;
while ($row=mysql_fetch_assoc($query)){
$out[] = array($counter, $row['total']);
$counter++;
}
Or, if you want your resulting $out
array have results indexed from 1
, instead of 0
, you could use something like this to set the index yourself :
$counter = 1;
while ($row=mysql_fetch_assoc($query)){
$out[$counter] = $row['total'];
$counter++;
}
Or any idea derived from this.
$i = 0;
while ($row=mysql_fetch_assoc($query)){
$out[] = array($i++, $row['total']);
}
精彩评论