php - 从 while 循环填充数组以返回 PHP

标签 php arrays return

嗨,我正在尝试使用“user1”和“score1”等索引填充数组以返回到函数。

我的代码:出现无法识别的变量错误

$query = 'select * from users order by score desc';
$result = mysql_query($query) or die (mysql_error());
$highscore = array();
$count = '0';

while($row = mysql_fetch_array($result))
{
    $count++;         
    $highscore = array('user' . $count => $row['username'], 'score' . $count => $row['score']);
}

return $highscore;

最佳答案

当您想要使用数值时,您应该使用它们。您使用了零的字符串表示形式 ('0')

$count = 0;
$highscore = array();
while($row = mysql_fetch_array($result))
{       
    $count++; 
    $highscore['user' . $count] = $row['username'];
    $highscore['score' . $count] = $row['score'];
}

关于php - 从 while 循环填充数组以返回 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728816/

相关文章:

arrays - 使用转置从列中删除 'nil'

javascript - console.log 与返回 : different results (JS)

c - C中的递归函数返回值而不返回

php - 使用导入上传 CSV 文件时,仅导入 1 条记录

php - 在移动设备上将当前 div 置于 Bootstrap 进度条的中心

c# - 如何编写一个可以用 `arr[key]` 索引的类(如数组)?

javascript - 使用 javascript 连接 url 的不同部分并添加分隔符(最后一个数组变量末尾除外)

php - 尝试访问 PHP 7.4 中 bool 类型值的数组偏移量

php - 打印适合页面的 html 表格

java - 该方法必须返回一个 int 类型