2014-10-01 4 views
0

У меня есть запрос к базе данных. В конце мне нужен объект JSON, как этотСоздание объекта json с массивом в php

 

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

Вот мой в то время как петля 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

+0

Почему вы смешиваете объекты и массивы вместе, когда вы можете просто создать ассоциативный массив для решения проблемы. – mithunsatheesh

+0

, потому что он не суммирует значения потраченного времени, не так ли? – Nikage

ответ

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; 
     } 
    } 
Смежные вопросы