0

我以这种格式从数据库中提取数据:

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;

    }
4

4 回答 4

2
while ($e = $db->fetch()) {
    $exerciseid = $e['exerciseid'];
    $id = $e['id'];
    $a[$exerciseid][$id] = $e;
}

或更短:

while ($e = $db->fetch()) {
    $a[$e['exerciseid']][$e['id']] = $e;
}

如果您不想要某些键,请使用unset()删除它们:

while ($e = $db->fetch()) {
    $exerciseid = $e['exerciseid'];
    $id = $e['id'];
    unset($e['id']);
    unset($e['exerciseid']);
    unset($e['date']);
    $a[$exerciseid][$id] = $e;
}
于 2013-06-14T08:59:17.157 回答
1
$a[$e['exerciseId']][$e['id']] = array_diff_key($e, array_flip(array('exerciseId', 'id', 'date')));

如果要将相同的数组分配给另一个数组,请执行$a = $b.
如果您只想要一些选择键,请使用array_intersect_key.
如果要省略某些键,请使用array_diff_key.

于 2013-06-14T08:55:09.223 回答
0
while($e = $db->fetch()) {
    foreach(array('time','weight','distance','reps','intensity') as $k){
        $a[$e['exerciseid']][$e['id']][$k] = $e[$k];
    }
}
于 2013-06-14T08:57:13.657 回答
0

尝试这个

function groupbyId(&$items,$group_by_key)
{        
    $newkey=0;
    $grouparr   = array();
    foreach ($items as $key => $val)
    {
          $grouparr[$val[$group_by_key]][$newkey]=$val; 
           $newkey++;       
    }  
    return $grouparr;  
}
$grouparr = groupbyId($items,"exerciseid");
print_r($grouparr);
于 2013-06-14T09:40:23.757 回答