douyong4842 2014-10-23 19:03
浏览 32
已采纳

PHP将int更改为string

I'm pulling data from a database table using php. I'm creating a simple string that provides information to a chart.

Here is the current code and out put:

$tsq7a = "SELECT * FROM FactDayUtilv1";
$stmt7a = sqlsrv_query( $conn, $tsq7a);

$data7a = "";                                                       
while( $row = sqlsrv_fetch_array( $stmt7a, SQLSRV_FETCH_NUMERIC)){  
$data7a .= "{name: '";
$data7a .= "$row[2]";
$data7a .= "', y: ";
$data7a .= "$row[3]";
$data7a .= ", drilldown: 'D";
$data7a .= "$row[2]";
$data7a .= "'},";
}
$data7a = substr($data7a,0,strlen($data7a)-1);

Creates the following output.

{name: '1', y: 65, drilldown: 'D1'},{name: '2', y: 66, drilldown: 'D2'},{name: '3', y: 54, drilldown: 'D3'},{name: '4', y: 71, drilldown: 'D4'},{name: '5', y: 43, drilldown: 'D5'},{name: '6', y: 12, drilldown: 'D6'},{name: '7', y: 8, drilldown: 'D7'}

I'm trying to replace the '1' '2' '3' '4' '5' '6' '7' in the output to the days of the week as below:

{name: 'Mon', y: 65, drilldown: 'D1'},{name: 'Tue', y: 66, drilldown: 'D2'},{name: 'Wed', y: 54, drilldown: 'D3'},{name: 'Thu', y: 71, drilldown: 'D4'},{name: 'Fri', y: 43, drilldown: 'D5'},{name: 'Sat', y: 12, drilldown: 'D6'},{name: 'Sun', y: 8, drilldown: 'D7'}

I've been trying str_replace or str_ireplace with no luck.

Any help appreciated.

Thanks Rob

  • 写回答

1条回答 默认 最新

  • douqie1884 2014-10-23 19:15
    关注

    create array;

    $days = array(1=>'Mon',2=>'Tue'); //..
    

    then change

    $data7a .= "$row[2]"; 
    

    to

    $data7a .= $days[$row[2]];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘