PHP:创建多维数组的更好方法?

时间:2013-06-14 08:51:18

标签: php arrays multidimensional-array

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

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] => 
)

现在我想合并这些数组,并在exerciseid匹配时创建多维数组。我做到了这一点:

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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

$a[$e['exerciseId']][$e['id']] = array_diff_key($e, array_flip(array('exerciseId', 'id', 'date')));

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

答案 2 :(得分:0)

while($e = $db->fetch()) {
    foreach(array('time','weight','distance','reps','intensity') as $k){
        $a[$e['exerciseid']][$e['id']][$k] = $e[$k];
    }
}

答案 3 :(得分: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);