简体   繁体   English

PHP将变量存储在数组中

[英]PHP Store variables in an array

I am trying to save the variable score in the $chartdata[0] array. 我试图将变量分数保存在$ chartdata [0]数组中。 If I put in a regular value let's say "20", i have no problem. 如果我输入一个常规值,我们说“ 20”,那我没问题。 However, when I try to reference the $score variable the values do not get stored in the array. 但是,当我尝试引用$ score变量时,值不会存储在数组中。 It works fine if I use the random function and even when I echo out score I can see my values coming out fine. 如果使用随机函数,效果很好,即使回显分数也可以看到我的值很好。 What am I doing wrong? 我究竟做错了什么?

while ($i < 5) {
  $date=mysql_result($result,$i,"date");
  $score=mysql_result($result,$i,"rating_score");
  echo $score;
  $chartData[0][] = $score;
  $chartData[1][] = rand(250,4000);
  $chartData[2][] = rand(250,4000);
  $categories[$i] = 'Label-' . $i;
  $i++;
}

The page is here: http://www.thelocalgolfer.com/chart/ 页面在这里: http : //www.thelocalgolfer.com/chart/

Not that I fully see the sense of your code (as you simply overwrite $chartData all the time so what's left when loop is done is last entry score and rands), but answering your question: instead of 并不是我完全了解您的代码含义(因为您$chartData一直覆盖$chartData ,所以循环完成后剩下的是最后输入分数和兰特),而是回答了您的问题:而不是

$chartData[0][] = $score;
$chartData[1][] = rand(250,4000);
$chartData[2][] = rand(250,4000);

do this 做这个

$chartData[0][] = $score;
$chartData[0][] = rand(250,4000);
$chartData[0][] = rand(250,4000);

When you are lost with arrays using print_r() or var_dump() on them usually helps to see what is really inside (i prefer print_r() if I do not care content, due to formatting) 当您迷失在数组上使用print_r()var_dump()时,通常可以帮助您了解真正的内容(由于格式问题,如果我不在乎内容,我更喜欢print_r()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM