Foros del Web » Programando para Internet » PHP »

funcion recursiva retornando array

Estas en el tema de funcion recursiva retornando array en el foro de PHP en Foros del Web. Hola amigos estoy teniendo un problema al recorrer una funcion de forma recursiva y que al fin al me retorne un array que se haya ...
  #1 (permalink)  
Antiguo 28/11/2012, 11:49
Avatar de chalchis  
Fecha de Ingreso: julio-2003
Mensajes: 1.773
Antigüedad: 20 años, 9 meses
Puntos: 21
Pregunta funcion recursiva retornando array

Hola amigos estoy teniendo un problema
al recorrer una funcion de forma recursiva y que al fin al
me retorne un array que se haya generado

Código PHP:
Ver original
  1. $listCustom = array();
  2.  
  3. function recursivo($equitativoZone)                                                        
  4. {
  5.                            
  6. global $listCustom;
  7.                                                                                
  8. foreach ($equitativoZone as $keyZ => $equitativoZoneAlias)
  9. {
  10.     echo $equitativoZone[$keyZ]['zona'].'<br>';
  11.     $listCustom[] = $equitativoZone[$keyZ]['zona'];
  12. }
  13.                                
  14. $hay = count($listCustom);
  15.                                                                                    
  16. if($hay < 16)
  17. {
  18.  echo "repite<br>";
  19.                                                                                
  20. recursivo($equitativoZone);
  21. }
  22. else
  23. {
  24.     echo "salir<br>";
  25.     echo count($listCustom);
  26.     //return $listCustom;
  27. }  
  28.  
  29. }

//llamando funcion
recursivo($equitativoZone);

echo "hay nuevo orden: ".count($listCustom)."<br>";

el problema es que quiero acceder ah ese array y me dice que hay cero elemento
use global como ultima opcion pero ni asi

me pueden sugerir como debo hacer esto?

saludos y gracias
espero sus comentarios
__________________
gerardo

Última edición por chalchis; 28/11/2012 a las 11:57
  #2 (permalink)  
Antiguo 28/11/2012, 22:28
Avatar de truman_truman  
Fecha de Ingreso: febrero-2010
Ubicación: /home/user
Mensajes: 1.341
Antigüedad: 14 años, 2 meses
Puntos: 177
Respuesta: funcion recursiva retornando array

en el caso de que entre aca:
if($hay < 16){

}
no hay ningún return, creo que por eso no hay resultados.


Aunque por otro lado yo veo resultados, fijate

Código PHP:
<?php 
$listCustom 
= array();
$equitativoZone[]['zona'] = 1;
$equitativoZone[]['zona'] = 2;
$equitativoZone[]['zona'] = 3;
$equitativoZone[]['zona'] = 4;
$equitativoZone[]['zona'] = 5;
$equitativoZone[]['zona'] = 6;
$equitativoZone[]['zona'] = 7;
$equitativoZone[]['zona'] = 8;
$equitativoZone[]['zona'] = 9;
/*$equitativoZone[]['zona'] = 10;
$equitativoZone[]['zona'] = 11;
$equitativoZone[]['zona'] = 12;
$equitativoZone[]['zona'] = 13;
$equitativoZone[]['zona'] = 14;
$equitativoZone[]['zona'] = 15;
$equitativoZone[]['zona'] = 16;
$equitativoZone[]['zona'] = 17;
$equitativoZone[]['zona'] = 18;
*/
function recursivo($equitativoZone)                                                         
{
                            
    global 
$listCustom;
                                                                                    
    foreach (
$equitativoZone as $keyZ => $equitativoZoneAlias){
        
        echo 
'-'.$listCustom[] = $equitativoZone[$keyZ]['zona'];
        echo 
'<br>';
    }
                                    
    echo 
$hay count($listCustom);
    echo 
'<br>';
                                                                                        
    if(
$hay 16){    
                                                                                 
        
recursivo($equitativoZone);
    
    }else{
    
        
        return 
$listCustom;
    }
     
 
}
echo 
'<pre>';
print_r($equitativoZone);

?>
__________________
la la la
  #3 (permalink)  
Antiguo 28/11/2012, 22:38
Avatar de chalchis  
Fecha de Ingreso: julio-2003
Mensajes: 1.773
Antigüedad: 20 años, 9 meses
Puntos: 21
Respuesta: funcion recursiva retornando array

gracias por darte el tiempo en responder
por algun motivo el

echo count($listCustom);

de la linea 25 me estaba haciendo algo raro

lo quite y resulta que ahora si accedo total del array uan vez
que se termino de recorrer recursivamente

saludos y gracias
__________________
gerardo
  #4 (permalink)  
Antiguo 28/11/2012, 22:51
Avatar de truman_truman  
Fecha de Ingreso: febrero-2010
Ubicación: /home/user
Mensajes: 1.341
Antigüedad: 14 años, 2 meses
Puntos: 177
Respuesta: funcion recursiva retornando array

podrías usar sizeof($listCustom) en lugar de count
__________________
la la la

Etiquetas: funcion, recursiva
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:00.