簡體   English   中英

將php數組轉換成逗號分隔的數組

[英]Turn a php array into comma separated array

我從一堆整數中獲取數據,我需要用逗號分隔以將它們用於chart.js數據集數據。 所以我在這里有數組:

if (mysqli_num_rows($result) > 0) {

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

echo json_encode($categories);

我需要將其轉換為json編碼的字符串:

我的數組回顯為:

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

我需要上面的輸出為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,
        }]
      }
    }); 

為什么不在PHP文件中將關聯數組更改為數字一,例如:

代替:

if (mysqli_num_rows($result) > 0) {

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

echo json_encode($categories);

采用:

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

希望對您有所幫助

為此,您可以使用Object.keys從對象中獲取鍵,然后使用這些鍵中的值填充數組:

 // Use this in production //var arr = <?php echo json_encode($categories, JSON_NUMERIC_CHECK); ?>; // For this example: var arr = [{ "respondent_sdo": "2", "respondent_dcto": "3", "respondent_ed": "5", "respondent_ca": "5", "respondent_dhpt": "3", "respondent_irt": "6", "respondent_gl": "2", "respondent_il": "5" }] var catData = []; Object.keys(arr[0]).forEach(function(key) { catData.push(parseInt(arr[0][key], 10)); }); console.log(catData); 

但是,我建議您改為執行邏輯來提取PHP代碼中的整數並將其提供給JS。

或使用數組映射功能。

var catData = JSON.parse(dataString);
var values ='';
values += catData.map(function(a) {
    return a.foo+',';
});

values = values.replace(/,\s*$/, ""); //to remove last coma

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM