如何在 PHP while 循环中查找数组的索引

3
我正在从MySQL数据库中获取数据... 我想检查每行的索引... 例如:
while($row=mysql_fetch_array($result)
{
i want index of $row in each row... (current Index).
}

请帮助我。 谢谢


3
循环体前的$i = 0;表示初始化变量$i为0;循环体内的$i++;表示在每次循环结束后将$i加1。 - zerkms
3个回答

20

如果您想从数据库中获取索引:

<?php
...
$result = mysql_query("SELECT * FROM whatever");

while($row = mysql_fetch_array($result)) {
    echo $row['index']; 
   # Where index is the field name containing the item ID from your database
}
...
?>

或者,如果您想基于输出计算项目数量:

<?php
..
$result = mysql_query("SELECT * FROM whatever");
$index = 0;

while($row = mysql_fetch_array($result)) {
    echo $index.' '.$row['whatever'];
    $index++;
}
..
?>

这是我从你的问题中理解到的内容...


4
如果我理解你的意思正确,你想要像这样的东西。
<?PHP
$i=0;
while($someThingIsTrue) {
   echo $i;
   $i++;
}
?>

0

$row不是任何数组的成员。因此,它根本没有索引。
而且,我想你也不需要它


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接