简体   繁体   English

使用php格式化数据输出

[英]Format data output using php

I have the following function which pulls data from the database :我有以下功能从数据库中提取数据:

public function get_dataset() {
        $dataset = "";
        $data_set = $this->operations_model->get_date();
        foreach ($data_set as $value) {
            $dataset .= '["' . $value['date'] . '","' . $value['revenue'] . '"],';
        }

        echo $dataset;
    }

And return data in the following format :并以以下格式返回数据:

["2015-10-13 15:53:20","15000.00"],["2015-10-13 15:53:20","5800.00"],["2015-10-13 15:53:20","5800.00"],["2015-10-13 15:53:20","5800.00"],["2015-10-13 15:54:56","15000.00"],

I would like to format the above data to be in the following format :我想将上述数据格式化为以下格式:

[[2015-10-13 15:53:20,15000.00],[2015-10-13 15:53:20,5800.00],[2015-10-13 15:53:20,5800.00],[2015-10-13 15:53:20,5800.00],[2015-10-13 15:54:56,15000.00]]

try this尝试这个

public function get_dataset() {
    $dataset = "[";
    $data_set = $this->operations_model->get_date();
    foreach ($data_set as $value) {
        $dataset .= '["' . $value['date'] . '","' . $value['revenue'] . '"],';
    }
    $dataset = substr($dataset, 0, -1);
    $dataset .= "]";

    echo $dataset;
}

try:尝试:

 public function get_dataset() { $dataset = ""; $comma=''; $data_set = $this->operations_model->get_date(); foreach ($data_set as $value) { $dataset .= $comma.'[' . $value['date'] . ',' . $value['revenue'] . ']'; $comma=','; } echo '['.$dataset.']'; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM