I'm pulling data from the DB in this format:
Array
(
[id] => 1
[exerciseid] => 127
[date] => 2013-06-12 00:00:00
[time] => 40271
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
Array
(
[id] => 2
[exerciseid] => 127
[date] => 2013-06-12 00:00:00
[time] => 120813
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
Now I want to merge these arrays and create multi-dimensional arrays if the exerciseid's match. I've done this:
Array
(
[127] => Array
(
[1] => Array
(
[time] => 40271
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
[2] => Array
(
[time] => 120813
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
)
)
My question is, is there a better way to write this than what I have?
while($e = $db->fetch()) {
foreach ($e as $key => $value) {
if($key == 'id')
$id = $value;
else if($key == 'exerciseid')
$exerciseid = $value;
else if($key == 'time')
$time = $value;
else if($key == 'weight')
$weight = $value;
else if($key == 'distance')
$distance = $value;
else if($key == 'reps')
$reps = $value;
else if($key == 'intensity')
$intensity = $value;
}
$a[$exerciseid][$id]['time'] = $time;
$a[$exerciseid][$id]['weight'] = $weight;
$a[$exerciseid][$id]['distance'] = $distance;
$a[$exerciseid][$id]['reps'] = $reps;
$a[$exerciseid][$id]['intensity'] = $intensity;
}
IF ELSE blocks
better do it inswitch Case
:)while($e = $db->fetch()) { $a[$e['exerciseid']][] = $e; }
? That's an one-liner that does the same thing.