简体   繁体   English

将php数组转换成逗号分隔的数组

[英]Turn a php array into comma separated array

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. 我从一堆整数中获取数据,我需要用逗号分隔以将它们用于chart.js数据集数据。 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: 我需要将其转换为json编码的字符串:

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

Why don't you change associative array into numeric one in PHP file like: 为什么不在PHP文件中将关联数组更改为数字一,例如:

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 希望对您有所帮助

To achieve this you can use Object.keys to get the keys from the object and then populate an array with the values from those keys: 为此,您可以使用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); 

However, I would suggest that you instead perform the logic to extract the integers in your PHP code and just provide them to the JS. 但是,我建议您改为执行逻辑来提取PHP代码中的整数并将其提供给JS。

Or use array map function. 或使用数组映射功能。

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