1

我知道在 php 中删除 char 的许多函数。但我很困惑删除这个:我想删除“,”

<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah 
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$data= "['$row[keyword]', $row[jumlah]],<br>";
echo $data;}
?> 

将给出结果:

['Smartfren', 1],
['Telkomsel', 1],

我想要这样的输出:

['Smartfren', 1],
['Telkomsel', 1]

怎么做?

非常感谢您的回复。

更新

根据@orangpill 的回答说我打印了如下数据:

['ABC',   6597],
['XYZ',   4479],
['PQR',   2075],
['Others', 450]

我想做一个图表,说js图表的代码必须是这样的:

data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                {
                    name: 'Chrome',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Safari',    8.5],
                ['Opera',     6.2],
                ['Others',   0.7]
            ]

现在基于@orangepill 我的代码:

  <?php
    include '../config/settings.php';
    $query = "SELECT keyword,count(*) AS jumlah 
    FROM search GROUP BY keyword ASC";
    $result = mysql_query($query);
    $data = array();
    while($row = mysql_fetch_array($result))
{
    $data[] = "['$row[keyword]', $row[jumlah]]";
}

echo implode(",<br/>", $data);
    ?>
    <script type="text/javascript">
      $(function () {
            $('#container').highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false
                },
                title: {
                    text: 'Presentase Sentimen Positif'
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                    percentageDecimals: 1
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            color: '#000000',
                            connectorColor: '#000000',
                            formatter: function() {
                                return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
                            }
                        }
                    }
                },
                series: [{
                    type: 'pie',
                    name: 'Nilai',
                    data: [


    <?php     while($row = mysql_fetch_array($result))
    {
        $data[] = "['$row[keyword]', $row[jumlah]]";
    }

    echo implode(",<br/>", $data); ?>
                    ]
                }]
            });
        });
    </script>
4

3 回答 3

2

通过构建一个数组并在您想要输出时将其内爆来执行此操作可能是有意义的

<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah 
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
    $data[] = "['$row[keyword]', $row[jumlah]]";
}

echo implode(",<br/>", $data);
?>

如果您正在格式化此数据以供 javascript 使用,则更好的方法是使用 json_encode。

<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah 
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
    $data[] = array($row[keyword], (int)$row[jumlah]);
}

echo "var data =  ".json_encode($data).";";
?> 
于 2013-07-21T03:39:52.850 回答
1

您还可以使用以下substr()功能:

$data = "";
while($row = mysql_fetch_array($result)) {
    $data .= "['$row[keyword]', $row[jumlah]],<br>";
}
$data = substr($data, 0, -5);
echo $data;
于 2013-07-21T03:46:22.757 回答
0

rtrim():

$data = rtrim($data, ',');

或者,在您的特定示例中,substr:

$data = substr($data, 0, -5);

删除“
”为好。

于 2013-07-21T03:39:24.390 回答