Q:

How can I make a triple foreach faster?

I have this http://pastebin.com/4NYvuaB0 view with the following result:

The first foreach iteracts with projects
the second iterates over Hitos (One project has one or more Hitos)
the third iterates over Tareas (One Hito has zero, one or more tareas)
It's working ok, but it takes about 120 seconds to render the view, which is too much!
Does someone have an idea of how to make it faster?
Thanks in advance.

A:

You should limit the use of class $semaforo leveraging on css
Change the range attribution, the one you have now is a bit confusing:
function semColor($p) {
    $semValues = array('rojo','berde','amarillo','naranja','rojo');
    $ranges = array(-1,50,70,100);
    for ($i=0; $i<4;$i++)
        if ($ranges[$i]>$p['desv']) break;
    return $semValues[$i];
}

use a template to create a row 
/* use any other method to create the template string */
$templ = '<div class="%s %s"><ul><li class="thmas">&nbsp;<a href="#" class="trigger2">&nbsp;</a></li>'
       . '<li class="'
       . join('">%s&nbsp;</li><li class="', 
             array_merge(array('','th1','th2','th3','th5','th4','th6'),
             array_fill(0,12,'thmes'),
             array('thtotal','thtotal)
       )).'">%s&nbsp;</li></ul></div>';

your code for the inner loop become:
foreach ( $aTareak as $a ) {
   if (($a['proyecto_id']==$h['proyecto_id']) && ($a['hito_id']==$h['hito_id'])) {
      printf($templ, 
         'divtrtarea',
         semColor($a['desv'])
         $a['categoria_nombre'],
         $a['coordinador_nombre'],
         $a['tarea_nombre'],
         $a['total_prev'],
         $a['total_horas'],
         round($a['desv'], 2),
         $a['1'], $a['2'], $a['3'], $a['4'], $a['5'], $a['6'],
         $a['7'], $a['8'], $a['9'], $a['10'], $a['11'], $a['12'],
         array_sum(array_slice($a,1,12)), /* assuming they are consecutive */
         $html->link('Selec.',array(
           'controller'=>'lineas',
           'action'=>'addhoras', 
           $a['proyecto_id'],
           $a['hito_id'],
           $a['tarea_id'])
         )
      );
   }
}

the outer ones have to change accordingly

