2014-10-01 136 views
0

我有一些數據庫查詢。 最後我需要這樣的在php中製作json對象數組

 

    [{ 
     activity_type:'Develoement', 
     spent_time:[ 
     {user:'User1', time:'21'}, // (!) amount of time for this user and this activity 
     {user:'User2', time:'2'}, 
     ], 
     total_spent_time: 23 
    },{ 
     activity_type:'Design', 
     spent_time:[ 
     {user:'User', time:'14'}, 
     ], 
     total_spent_time: 14 
    }] 

這裏JSON對象是我的SQL查詢

 

    while($row = $result->fetch_assoc()){ 

       $data[$row['activity_type']] = array('spent_time' => 
        array('user' => (object)$row['user'])); 
       $data[$row['activity_type']]['spent_time']['user']->time += $row['spent_time']; 

      } 

,而循環,但在結果數組我有

 

     [Design] => Array 
     (
      [spent_time] => Array 
       (
        [user] => stdClass Object 
         (
          [scalar] => John 
          [time] => 1.5 // (!) only the last activity time, NOT the sum of spent time for this activity 
         ) 

       ) 

     ) 

    [Developement] => Array 
     (
      [spent_time] => Array 
       (
        [user] => stdClass Object 
         (
          [scalar] => Nick 
          [time] => 0.3 // (!) only the last activity time, NOT the sum of spent time for this activity 
         ) 

       ) 

     ) 

如何我是否將當前用戶和當前活動的循環中的時間值相加?

UPD 下面是該查詢

$sql = "SELECT 
       distinct(projects.name) as project, 
       time_entries.hours as spent_time, 
       enumerations.name as activity_type, 
       versions.name as version, 
       users.lastname as user 
       FROM `projects` 
       INNER JOIN time_entries ON (time_entries.project_id = projects.id) 
       INNER JOIN enumerations ON (time_entries.activity_id = enumerations.id) 
       INNER JOIN versions ON (projects.id = versions.project_id) 
       inner join users on (time_entries.user_id = users.id) 
       where (projects.identifier = 'test')"; 
+0

顯示您的查詢。 – Manwal 2014-10-01 04:27:32

+0

爲什麼要將對象和數組混合在一起,只需製作一個關聯數組即可解決問題。 – mithunsatheesh 2014-10-01 04:33:50

+0

,因爲它沒有總結花費時間的值,是嗎? – Nikage 2014-10-01 04:39:38

回答

0

也許像...

class ActivityList{ 
     private $activities; 

     public function __construct(){ 
      $this->activities=array(); 
     } 

     public function addActivity($row){ 
      //read the row, search for the activity 
      //if does not exist, create a new activity 
      //if does, get it and use addUser to add more time 
      ... 
     } 
    } 

    class Activity{ 
     private $activity_type; 
     private $spent_time; 
     private $total_spent_time; 

     public function __construct(){ 
      $this->spent_time=array(); 
      $this->activity_type=""; 
      $this->total_spent_time=0; 
     } 

     public function addUser($user, $timeSpent){ 
      //search for the user in spent_time 
      //if not exist, create a new user 
      //if exist, add the time 
      //once added, take the time and add to total_spent_time 
      ... 
     } 
    } 

    class UserObject{ 
     private $user; 
     private $time; 

     public function __construct(){ 
      $this->user=""; 
      $this->time=0; 
     } 
    }