0

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;

    }
4
  • 1
    wow! thats a lot of IF ELSE blocks better do it in switch Case :)
    – Þaw
    Commented Jun 14, 2013 at 8:54
  • Use "case" instead of "if else"
    – maximkou
    Commented Jun 14, 2013 at 8:54
  • 1
    Did you try while($e = $db->fetch()) { $a[$e['exerciseid']][] = $e; } ? That's an one-liner that does the same thing.
    – Jon
    Commented Jun 14, 2013 at 8:55
  • Why can't you take the result GROUP BY 'exerciseid' and simply a lil? Commented Jun 14, 2013 at 8:56

4 Answers 4

2
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;
}
1
$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.

2
  • your missing date in your array_flip
    – DevZer0
    Commented Jun 14, 2013 at 8:57
  • 1
    I voted for your answer because it is way much shorter even if it is less readable. Also the array_diff_key - array_flip trick is more advanced PHP scripting in my opinion. Maybe a little hard to understand but very elegant to a purist.
    – Jérôme
    Commented Jun 14, 2013 at 9:25
0
while($e = $db->fetch()) {
    foreach(array('time','weight','distance','reps','intensity') as $k){
        $a[$e['exerciseid']][$e['id']][$k] = $e[$k];
    }
}
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.