简体   繁体   中英

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.']'; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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