如何将json编码更改为数组字符串

我的任务是解决如何将 JSON 编码更改为数组字符串,请查看下面的代码:我有


["11:00"]["09:00"]["01:00"]["12:00"]["11:00"]["10:00"]["01:00"]["00:00"]["23:00"]["22:00"]

如何改变它成为


["11:00","09:00","01:00","11:00","10:00","01:00","00:00","23:00","22:00"]

我的stats.php样子是这样的:


out .=  '         <div class="col-lg-5 col-md-5 col-sm-5 col-lg-offset-1 col-md-offset-1 col-sm-offset-1" style="height: 300px">';

        $out .=  '          <div class="chartjs-size-monitor">';

        $out .=  '              <div class="chartjs-size-monitor-expand">';

        $out .=  '                  <div class=""></div>';

        $out .=  '              </div>';

        $out .=  '              <div class="chartjs-size-monitor-shrink">';

        $out .=  '                  <div class=""></div>';

        $out .=  '              </div>';

        $out .=  '         </div>';

        $out .=  '         <canvas id="myChart2" class="canpas chartjs-render-monitor" style="display: block; width: 454px; height: 300px;" width="454" height="300"></canvas>';

        $out .=  '        </div>';


$graph_query = mssql_query("SELECT TOP 100 Convert(nvarchar,dtDate,108) AS Date, serial, nAverageUser, nMaxUser FROM tbl_ServerUser_Log ORDER BY serial DESC");


            $i = 1;

            while ($graph = mssql_fetch_array($graph_query)) {

                $graph['nMaxUser'] = round($graph['nMaxUser'] * $percent_inc);

                $graph['nAverageUser'] = round($graph['nAverageUser'] * $percent_inc);

                $serie1->addPoint(new Point($graph['Date'], $graph['nMaxUser']));

                $serie2->addPoint(new Point($graph['Date'], $graph['nAverageUser']));

                $date = date('H:i', strtotime($graph['Date']));

                $convert = json_encode(str_split($date, 5));

                // $convert = str_replace('][','',$convert);


                echo $convert;


            }

我只想显示从手动值到从 SQL 服务器捕获的查询值的图表。

预先感谢您回答我的问题。


慕森王
浏览 88回答 1
1回答

牛魔王的故事

不要每次循环都回显 JSON。将时间放在一个数组中,并在末尾回显 JSON。$dates = [];while ($graph = mssql_fetch_array($graph_query)) {&nbsp; &nbsp; $graph['nMaxUser'] = round($graph['nMaxUser'] * $percent_inc);&nbsp; &nbsp; $graph['nAverageUser'] = round($graph['nAverageUser'] * $percent_inc);&nbsp; &nbsp; $serie1->addPoint(new Point($graph['Date'], $graph['nMaxUser']));&nbsp; &nbsp; $serie2->addPoint(new Point($graph['Date'], $graph['nAverageUser']));&nbsp; &nbsp; $date = date('H:i', strtotime($graph['Date']));&nbsp; &nbsp; $dates[] = $date;&nbsp; &nbsp; echo $convert;}echo json_encode($dates);
打开App,查看更多内容
随时随地看视频慕课网APP