简体   繁体   中英

PHP Store variables in an array

I am trying to save the variable score in the $chartdata[0] array. If I put in a regular value let's say "20", i have no problem. However, when I try to reference the $score variable the values do not get stored in the array. 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/

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[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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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