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')";
Показать запрос. – Manwal
Почему вы смешиваете объекты и массивы вместе, когда вы можете просто создать ассоциативный массив для решения проблемы. – mithunsatheesh
, потому что он не суммирует значения потраченного времени, не так ли? – Nikage