doushenken2833 2016-11-22 09:20 采纳率: 0%
浏览 113
已采纳

将php数组转换为逗号分隔数组

I am getting data from a row which are a bunch of integers and I need to comma separate them to use them for a chart.js dataset data. So I have got the array here:

if (mysqli_num_rows($result) > 0) {

  $categories = [];
  while($row = mysqli_fetch_assoc($result)) {
    $categories[] = $row;     
  }

echo json_encode($categories);

I need to turn it into json encoded string:

My array echos out as:

[{"respondent_sdo":"2","respondent_dcto":"3","respondent_ed":"5","respondent_ca":"5","respondent_dhpt":"3","respondent_irt":"6","respondent_gl":"2","respondent_il":"5"}]

To which I need the above to output as 2,3,5,5,3,6,5

var dataString= "<?php echo json_encode($categories, JSON_NUMERIC_CHECK);  ?>";
var catData = JSON.parse(dataString);


    var ctx = document.getElementById("myChart1");
    var myChart1 = new Chart(ctx, {
      type: 'radar',
        options: {
             legend: {
                display: true
             },
             tooltips: {
                enabled: false
             },
            scale: {
                ticks: {
                    beginAtZero: true,
                    stepSize: 1
                }
            }         
        },  
      data: {
        labels: ["Strategic Development and Ownership", 
                 "Driving change through others", 
                 "Exec Acumen", 
                 "Commercial Acumen", 
                 "Develops High Performance Teams", 
                 "Innovation and risk taking", 
                 "Global Leadership", 
                 "Industry Leader"
                 ],


        datasets: [{
          label: 'Ian Galbraith',
          backgroundColor: "rgba(255,6255,255,0)",
          borderColor: "lightblue",
          data: catData,
        }]
      }
    }); 
  • 写回答

3条回答 默认 最新

  • dop82210 2016-11-22 09:38
    关注

    Why don't you change associative array into numeric one in PHP file like:

    Instead of:

    if (mysqli_num_rows($result) > 0) {
    
      $categories = [];
      while($row = mysqli_fetch_assoc($result)) {
        $categories[] = $row;     
      }
    
    echo json_encode($categories);
    

    Use:

    if (mysqli_num_rows($result) > 0) {
    
      $categories = [];
      while($row = mysqli_fetch_assoc($result)) {
        $categories[] = $row;     
      }
    $response = array();
    foreach($categories as $c){
       foreach($c as $val){
          $response[] = (int)$val;
       }
    }
    echo json_encode($response);
    

    I hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 求fpga交通信号灯设计Verilog代码
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头