php 如何在php while循环中找到数组的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7545323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:55:34  来源:igfitidea点击:

How to find Index of an array in php while loop

php

提问by zzzzz

I am fetching data from MySQL database... I want to check index of each row... e.g

我正在从 MySQL 数据库中获取数据......我想检查每一行的索引......例如

while($row=mysql_fetch_array($result)
{
i want index of $row in each row... (current Index).
}

Please Help me. Thanks

请帮我。谢谢

回答by Andrei Durduc

If you want the index from the database:

如果你想要数据库中的索引:

<?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
}
...
?>

Or, if you want to count the number of items based on the output:

或者,如果您想根据输出计算项目数:

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

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

That's what I understood from your question..

这就是我从你的问题中了解到的..

回答by ivarni

If I understood you correctly you want something like this

如果我理解正确,你想要这样的东西

<?PHP
$i=0;
while($someThingIsTrue) {
   echo $i;
   $i++;
}
?>

回答by Your Common Sense

$row is not a member of any array. So, it has no index at all.
And, I suppose, you don't need it either

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