drazvzi741287 2015-06-27 09:35
浏览 17

使用php将列与数组分开

I am trying to plot bar graph using chart.js and from mssql database.I am able to retrieve data from the database in array.I have used following code,

<?php    
$serverName = "PC";   
$uid = "sa";     
$pwd = "PC#1234";    
$databaseName = "climate";   
$dsn = "sql"; 

$conn= odbc_connect ($dsn ,$uid ,$pwd);
if(!$conn){
    echo('Connection Failed');
}
$sql="select convert(nvarchar(100), DVCDTxp_Datetime,106)   )";
$rs=odbc_exec($conn,$sql);
if (!$rs){
    exit("Error in SQL");
}

echo odbc_result_all($rs);

$response = array();
for($i=1;$i<= odbc_num_rows($rs);$i++){
    $row = odbc_fetch_array($rs,$);
    $response[] = $row;
}
?>

I am getting following output,

DATE            AVG_VAL
06 Jun 2015     5.466593   
08 Jun 2015     1.774121 
09 Jun 2015     .729223
11 Jun 2015     3.329457 
12 Jun 2015     2.344660 

I want the following output ,

DATE=["06 Jun 2015","08 Jun 2015","09 Jun 2015","11 Jun 2015","12 Jun 2015"]
VALUE=["5.466593","1.774121",".729223","3.329457","2.344660"]
  • 写回答

3条回答 默认 最新

  • duangangmo0583 2015-06-27 09:47
    关注

    You are creating array $response so assuming array keys to be indexed and starting from zero.

    If not, use array_values() inside json_encode() while echoing.

    $all_dates = array_map(function($val){ return $val['DATE']; }, $response);
    
    $all_values = array_map(function($val){ return (float)$val['AVG_VAL']; }, $response);
    

    Use as,

    DATE = <?php echo json_encode($all_dates)?>
    
    VALUE = <?php echo json_encode($all_values)?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch