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')";
顯示您的查詢。 – Manwal 2014-10-01 04:27:32
爲什麼要將對象和數組混合在一起,只需製作一個關聯數組即可解決問題。 – mithunsatheesh 2014-10-01 04:33:50
,因爲它沒有總結花費時間的值,是嗎? – Nikage 2014-10-01 04:39:38