我以这种格式从数据库中提取数据:
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] =>
)
现在我想合并这些数组并创建多维数组,如果运动 ID 匹配。我已经这样做了:
Array
(
[127] => Array
(
[1] => Array
(
[time] => 40271
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
[2] => Array
(
[time] => 120813
[weight] =>
[distance] => 1000
[reps] =>
[intensity] =>
)
)
)
我的问题是,有没有比我现有的更好的方法来写这个?
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;
}