Ver Mensaje Individual
  #3 (permalink)  
Antiguo 30/07/2009, 09:07
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Recorrer tres arreglos al mismo tiempo

Hace poco hice este script para moverme por muchos arrays al mismo tiempo:
Código PHP:
Ver original
  1. <?php
  2. $items1 = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  3. $items2 = array( 2, 4, 6 );
  4. $items3 = array( 10, 20, 30, 40 );
  5. $items4 = array( 100, 200, 300, 400, 500, 600, 700 );
  6.  
  7. echo "<table border=1><tr>";
  8. while(true) {
  9.     // Fetch Values
  10.     $item1 = current($items1);
  11.     $item2 = current($items2);
  12.     $item3 = current($items3);
  13.     $item4 = current($items4);
  14.    
  15.     // Print Values
  16.     echo "<td>" . (( $item1 !== false) ? $item1 : "&nbsp;") . "</td>";
  17.     echo "<td>" . (( $item2 !== false) ? $item2 : "&nbsp;") . "</td>";
  18.     echo "<td>" . (( $item3 !== false) ? $item3 : "&nbsp;") . "</td>";
  19.     echo "<td>" . (( $item4 !== false) ? $item4 : "&nbsp;") . "</td>";
  20.    
  21.     // Up! Next Value
  22.     $item1 = next( $items1 );
  23.     $item2 = next( $items2 );
  24.     $item3 = next( $items3 );
  25.     $item4 = next( $items4 );
  26.    
  27.     // Check terminator
  28.     if($item1 === false && $item2 === false && $item3 === false && $item4 === false) break;
  29.     echo "</tr><tr>";
  30. }
  31. echo "</tr></table>";
  32.  
  33. echo sprintf("Done! Max array size is %s.", get_max_count($items1, $items2, $items3, $items4));
  34.  
  35. function get_max_count() {
  36.     $nMax = 0;
  37.     foreach(func_get_args() as $param ) {
  38.         if(is_array($param)) {
  39.             $nSize = count($param);
  40.             if($nSize > $nMax) {
  41.                 $nMax = $nSize;
  42.             }
  43.         }
  44.     }
  45.    
  46.     return $nMax;
  47. }

Saludos.