2013-11-09 61 views
0

我有下面的查詢返回的日期(字符串值)和數字(整數)查詢應該返回整數,字符串CI

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(); 
} 

但是,當我在我的傾銷結果在我的控制,我得到一個只有字符串的數組。值應該是整數,以便將它們用於圖形。

只是第一片陣列的:

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" 
    } 

我處理這個陣列創建一個JSON對象:

$_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)); 

但JSON對象包含的值作爲一個字符串,而不是像像所需的整數。我應該改變什麼?

JSON輸出的樣本:

{"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"}]} 

誰能幫我在這一個?

回答

0

嘗試這樣的事情

嘗試檢查,如果值Integer.If它是整數比它解析字符串

 foreach ($rows as $i => $row) { 
     foreach ($row as $column => $value) { 
      if(is_numeric($value)){ 
       $_rows[$i][$column] = intval($value); 
      }else{ 
       $_rows[$i][$column] = $value; 
      } 
     } 
    } 
+0

非常感謝你的幫助,這是解決方案!我的圖正在工作! – mastahb

相關問題