I’ve got sql database table which looks like this:
login | time_start |
---|---|
name1 | 17:30:00 |
name2 | 18:15:00 |
name3 | 16:30:00 |
name2 | 17:30:00 |
And i want to convert it into array with hour as id associated with all logins coresponding to that hour:
Array (
[ 18:15:00 ] => Array ( [login] => name2 )
[ 17:30:00 ] => Array ( [login] => (name1, name3) )
[ 16:30:00 ] => Array ( [login] => name3 )
)
I tried something like this:
if ($result && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$aReturn[$row['time_start']] = array(
'login' => $row['login']
);
}
}
but it returned only 3 rows with only one login:
Array (
[16:30:00] => Array ( [login] => name3 )
[17:30:00] => Array ( [login] => name1 )
[18:15:00] => Array ( [login] => name2 )
)
2
Answers
RESULT should be like
OR, if you didnt mean you wanted to reduce the Time to only the Hour then simply
Note removed the IF as if there is nothing to do the wjile loop will do nothing which is the same result as with the IF there.
This will do it
you could do this for clarity …
but it’s not functionally necessary in php