Foros del Web » Programando para Internet » PHP »

Bucle que Recorre Array

Estas en el tema de Bucle que Recorre Array en el foro de PHP en Foros del Web. Tengo un array similar a este, y necesito extraer los datos que se encuentran en las posiciones ["text"]. Lo que no termino de determinar es ...
  #1 (permalink)  
Antiguo 13/06/2012, 15:26
Avatar de Heiroon  
Fecha de Ingreso: junio-2010
Ubicación: Caracas, Venezuela - Por ahora...
Mensajes: 495
Antigüedad: 13 años, 10 meses
Puntos: 63
Exclamación Bucle que Recorre Array

Tengo un array similar a este, y necesito extraer los datos que se encuentran en las posiciones ["text"]. Lo que no termino de determinar es la lógica del bucle que tengo que construir para recorrerlo e ir guardando los datos en un string.

A ver si me pueden dar alguna seña... Muchas gracias de antemano...

Código PHP:
Ver original
  1. array(1)
  2.     {  
  3.     [0]=> array(4)
  4.     {
  5.             ["name"]=> string(7) "eventos"
  6.             ["text"]=> NULL
  7.             ["attributes"]=> array(1)
  8.             {
  9.                 ["gml:id"]=> string(6) "F27__4"
  10.             } ["children"]=> array(11)
  11.             {
  12.                 ["C1C_Mobile_c1c_mobile:objectid"]=> array(1)
  13.                 {
  14.                     [0]=> array(4)
  15.                     {
  16.                         ["name"]=> string(8) "objectid"
  17.                         ["text"]=> string(1) "4"
  18.                         ["attributes"]=> array(0) { }
  19.                         ["children"]=> array(0) { }
  20.                     }
  21.                 }
  22.                 ["C1C_Mobile_c1c_mobile:tutulo_del_evento"]=> array(1)
  23.                 {
  24.                     [0]=> array(4)
  25.                     {
  26.                         ["name"]=> string(17) "tutulo_del_evento"
  27.                         ["text"]=> string(32) "CONFORMACIÓN DE CAMPO DEPORTIVO"
  28.                         ["attributes"]=> array(0) { }
  29.                         ["children"]=> array(0) { }
  30.                     }
  31.                 }
  32.                 ["C1C_Mobile_c1c_mobile:direccion"]=> array(1)
  33.                 {
  34.                     [0]=> array(4)
  35.                     {
  36.                         ["name"]=> string(9) "direccion"
  37.                         ["text"]=> string(24) "La Charneca, San Agustin"
  38.                         ["attributes"]=> array(0) { }
  39.                         ["children"]=> array(0) { }
  40.                     }
  41.                 }
  42.                 ["C1C_Mobile_c1c_mobile:descripcion"]=> array(1)
  43.                 {
  44.                     [0]=> array(4)
  45.                     {
  46.                         ["name"]=> string(11) "descripcion"
  47.                         ["text"]=> string(53) "la Alcaldía Metropolitana traslada 2 Tanques de Agua"
  48.                         ["attributes"]=> array(0) { }
  49.                         ["children"]=> array(0) { }
  50.                     }
  51.                 }
  52.                 ["C1C_Mobile_c1c_mobile:fecha"]=> array(1)
  53.                 {
  54.                     [0]=> array(4)
  55.                     {
  56.                         ["name"]=> string(5) "fecha"
  57.                         ["text"]=> string(19) "2011-04-08T21:55:34"
  58.                         ["attributes"]=> array(0) { }
  59.                         ["children"]=> array(0) { }
  60.                     }
  61.                 }
  62. }
  63.  
  64. }

Nota: Son aprox. 1660 registros como este y por tanto, quiero hacer un script lo mas eficiente que se pueda. Recorte el arreglo para mejor visualizacion, pero tiene mas datos.
__________________
Gmail : [email protected]
Twitter: @heiroon

I'm back!
  #2 (permalink)  
Antiguo 13/06/2012, 15:32
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: Bucle que Recorre Array

Código PHP:
Ver original
  1. function extrae($ar,$text){
  2.    foreach($ar as $key =>$v){
  3.       if(is_array($v))
  4.           $text[]=extrae($v,$text);
  5.       if($k=='text')
  6.          $text[]=$v;
  7.    }
  8.    return $text;
  9. }
Sin probar la función, creo que la lógica se entiende, aunque no estoy seguro de que lo necesites así de simple.

Suponiendo que dejas los textos es un arreglo, a menos que quieras concatenarlos.
  #3 (permalink)  
Antiguo 13/06/2012, 15:33
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Bucle que Recorre Array

No es un array tan complejo, me parece que su estructura es recursiva, una función que haga un recorrido simple debe funcionar.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #4 (permalink)  
Antiguo 13/06/2012, 16:25
Avatar de Heiroon  
Fecha de Ingreso: junio-2010
Ubicación: Caracas, Venezuela - Por ahora...
Mensajes: 495
Antigüedad: 13 años, 10 meses
Puntos: 63
De acuerdo Respuesta: Bucle que Recorre Array

Bueno... Esta parte la resolví efectivamente con un foreach simple.

Código PHP:
Ver original
  1. $evento = $registers[$i]["children"]["C1C_Mobile_c1c_mobile:eventos"];
  2.             $nXML = var_export($evento, true);
  3.  
  4.                 $id = $evento[0]["attributes"]["gml:id"];
  5.                 $cod_capa = explode("_", $id);
  6.            
  7.                 foreach ($evento[0]["children"] as $clave => $valor)
  8.                 {
  9.                     print $valor[0]["text"]."<br/>";
  10.                    
  11.                 }

El problema me lo consigo en un array de nivel inferior..

Código PHP:
Ver original
  1. ["C1C_Mobile_c1c_mobile:shape"]=> array(1)
  2.                 {
  3.                     [0]=> array(4)
  4.                     {
  5.                         ["name"]=> string(5) "shape"
  6.                         ["text"]=> NULL
  7.                         ["attributes"]=> array(0) { }
  8.                         ["children"]=> array(1)
  9.                         {
  10.                             ["gml:point"]=> array(1)
  11.                             {
  12.                                 [0]=> array(4)
  13.                                 {
  14.                                     ["name"]=> string(5) "point"
  15.                                     "text"]=> NULL ["attributes"]=> array(0) { }
  16.                                     ["children"]=> array(1)
  17.                                     {
  18.                                         ["gml:pos"]=> array(1)
  19.                                         {
  20.                                             [0]=> array(4)
  21.                                             {
  22.                                                 ["name"]=> string(3) "pos"
  23.                                                 ["text"]=> string(38) "10.495218384609036 -66.899088900162212"
  24.                                                 ["attributes"]=> array(0) { }
  25.                                                 ["children"]=> array(0) { }
  26.                                             }
  27.                                         }
  28.                                     }
  29.                                 }
  30.                             }
  31.                         }
  32.                     }
  33.                 }

Que de verdad no se como entrarle... Saludos!
__________________
Gmail : [email protected]
Twitter: @heiroon

I'm back!
  #5 (permalink)  
Antiguo 13/06/2012, 16:30
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: Bucle que Recorre Array

Pues necesitas usar recursividad, como en el ejemplo que te puse.
  #6 (permalink)  
Antiguo 13/06/2012, 16:43
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Bucle que Recorre Array

Puedes utilizar iterators, algo asi:

Código PHP:
Ver original
  1. $it = new RecursiveIteratorIterator(
  2.         new RecursiveArrayIterator($array),
  3.         RecursiveIteratorIterator::CHILD_FIRST
  4.     );
  5. foreach($it as $key => $node){
  6.     if('text' === $key){
  7.         echo $node . PHP_EOL;
  8.     }  
  9. }

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)

Etiquetas: bucle, string
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 03:08.