ditto
ditto

Reputation: 6277

PHP: Better way to create multi-dimensional arrays?

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;

    }

Upvotes: 0

Views: 132

Answers (4)

Krishnarag
Krishnarag

Reputation: 56

Try this

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);

Upvotes: 0

Jérôme
Jérôme

Reputation: 2090

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

or shorter :

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

and if you don't want some keys, use unset() to remove them :

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

Upvotes: 2

Mmmh mmh
Mmmh mmh

Reputation: 5470

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

Upvotes: 0

deceze
deceze

Reputation: 522135

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

If you're assigning the same array to another array, do $a = $b.
If you only want some select keys, use array_intersect_key.
If you want to omit certain keys, use array_diff_key.

Upvotes: 1

Related Questions