0

I have got the following query which returns Dates (string value) and Numbers (integer value)

public function getGraphDataPositives() {       
    $query = 'SELECT 
    DATE(stamp) AS x, SUM(positive) AS y
    FROM data 
    WHERE companyId = 3 
    GROUP BY DATE(stamp)';

    $currentDate = date("%Y-m-d%");
    $query = $this->db->query($query);
    return $query->result();
}

But when I'm dumping my result in my controller I get an array with only strings. The values should be integer in order to use them for a graph.

Just the first piece of array:

array(25) {
  [0]=>
  object(stdClass)#169 (2) {
    ["x"]=>
    string(10) "2013-10-16"
    ["y"]=>
    string(7) "3283581"
  }
  [1]=>
  object(stdClass)#160 (2) {
    ["x"]=>
    string(10) "2013-10-17"
    ["y"]=>
    string(7) "1512116"
  }

I'm handling this array to create a Json object:

    $_rows = array();

    foreach ($rows as $i => $row) {
        foreach ($row as $column => $value) {
            $_rows[$i][$column] = $value;
        }
    }       

    $rows = $_rows;         

    echo json_encode(array("className" => ".main.l1","data" => $rows));

But the JSON object contains the values as a string, not like an integer like desired. What should I change?

Sample of JSON output:

    {"className":".main.l1","data":[{"x":"2013-10-16","y":"3283581"},{"x":"2013-10-17","y":"1512116"},{"x":"2013-10-18","y":"3967"},{"x":"2013-10-19","y":"1094"},{"x":"2013-10-20","y":"853"},{"x":"2013-10-21","y":"1205"},{"x":"2013-10-22","y":"2618700"},{"x":"2013-10-23","y":"3928291"},{"x":"2013-10-24","y":"3670318"},{"x":"2013-10-25","y":"3347369"},{"x":"2013-10-26","y":"2525573"},{"x":"2013-10-27","y":"3224612"},{"x":"2013-10-28","y":"3992964"},{"x":"2013-10-29","y":"3949904"},{"x":"2013-10-30","y":"3568618"},{"x":"2013-10-31","y":"3104696"},{"x":"2013-11-01","y":"3246932"},{"x":"2013-11-02","y":"2817758"},{"x":"2013-11-03","y":"3198856"},{"x":"2013-11-04","y":"3952957"},{"x":"2013-11-05","y":"3934173"},{"x":"2013-11-06","y":"3878718"},{"x":"2013-11-07","y":"3642822"},{"x":"2013-11-08","y":"3388646"},{"x":"2013-11-09","y":"376763"}]}

Can anyone help me out on this one?

4

1 回答 1

0

尝试这样的事情

尝试检查值是否为整数。如果它是整数而不是将其解析为字符串

     foreach ($rows as $i => $row) {
        foreach ($row as $column => $value) {
            if(is_numeric($value)){
                $_rows[$i][$column] = intval($value);
            }else{
                $_rows[$i][$column] = $value;
            }
        }
    } 
于 2013-11-09T08:53:04.153 回答