将已分配的有序数添加到数组条目php

时间:2010-04-08 20:40:32

标签: php mysql

嘿伙计们我从mysql结果中分配了一个数组,我只想从一开始编号。有谁知道怎么做?

 while ($row=mysql_fetch_assoc($query)){
$out[] = array("ASSIGNED INTEGER", $row['total']);        
 }

2 个答案:

答案 0 :(得分:2)

您必须使用变量作为计数器,以跟踪您所在的行:

$counter = 1;
while ($row=mysql_fetch_assoc($query)){
    $out[] = array($counter, $row['total']);
    $counter++;
}


或者,如果您希望生成的$out数组的结果从1开始,而不是0,则您可以使用类似的内容自行设置索引:

$counter = 1;
while ($row=mysql_fetch_assoc($query)){
    $out[$counter] = $row['total'];
    $counter++;
}

或者从中得出任何想法。

答案 1 :(得分:1)

$i = 0;
while ($row=mysql_fetch_assoc($query)){
    $out[] = array($i++, $row['total']);        
}