-2

我的两个可爱的 JSON 数组有一个很大的问题。

代码如下:

else if ($range == "day")   $sqlRangeString = "GROUP BY DATE_FORMAT(dbtime, '%Y-%c-%e')";

$startdate  = doTimeStamp($startdate);
$enddate    = doTimeStamp($enddate);

  if (isset($consumer_on))
  {
    echo '[';
    $counter = 0;    
    foreach($consumer_name as $consumer_name_value)
    {           
    $result2 = mysql_query("SELECT AVG(loadvalue) AS m1, dbtime, time 
                FROM $consumer_name_value           
                WHERE time >= $startdate 
                AND time <= $enddate 
                $sqlRangeString
                ORDER BY time") or die('#ä');   
    while ($data2 = mysql_fetch_array($result2))
    {       
      $consumer_value[$counter][0] = $data2['time'];        
      if (!isset($consumer_value[$counter][1]))  $consumer_value[$counter][1] = 0;  
       $consumer_value[$counter][1] =   (float)$consumer_value[$counter][1] +  (float)$data2['m1'];     
      $counter++;      
    }       
    }
    echo json_encode($consumer_value); 
   }

  if (isset($producer_on))
  {
    $ounter = 0;
    if (isset($consumer_on) && ($consumer_on == 1))echo ',';
    foreach($producer_name as $producer_name_value)
    { 
    $result3 = mysql_query("SELECT AVG(power) AS m1, dbtime, time 
                FROM $producer_name_value           
                WHERE time >= $startdate 
                AND time <= $enddate 
                $sqlRangeString
                ORDER BY time") or die('#ää');   ;      

      while ($data3 = mysql_fetch_array($result3))
      {     
      $producer_value[$counter][0] = $data3['time'];
     if (!isset($producer_value[$counter][1]))  $producer_value[$counter][1] = 0;
      $producer_value[$counter][1] =   $producer_value[$counter][1] +  (float)$data3['m1'];   
      $counter++;
      }      
  }
  echo json_encode($producer_value);    
  }
}

?>

我的问题是输出:

在此处输入图像描述

为什么 produver_value 数组的输出在每个部分前面都有数字?我怎样才能删除这个?

4

1 回答 1

2

“数组”在每个项目之前都有一个数字,因为 PHP 数组索引不是从 0 开始的。您有一个关联数组而不是常规数组,这使得json_encode生成 JSON 对象而不是数组。

这可能是由于这个错字而发生的:

$ounter = 0; // should be $counter ?

您可以使用array_values从数组中提取值。

于 2013-06-11T12:21:50.317 回答