![]() |
| |||||||
| Registrarse | Preguntas Frecuentes | Lista de Foreros | Calendario | Buscar | Temas de Hoy | Marcar Foros Como Leídos |
![]() |
| | LinkBack | Herramientas | Desplegado |
| |||
| I was reading this article yesterday: http://www.talkphp.com/advanced-php-...rray-item.html I am wondering.. okay.. we can use array_walk but doesn't that just loop through the items anyway ?? So why not just use the foreach loop instead... is it a shorthand trick or is there any deeper reasoning behind it... Thats my question! I do have another question and that is if it is a shorthand version, having a foreach loop isn't much more in terms of the letters you type and so why have the PHP developers put it in there ?? It's puzzling me. Thank you in advance. |
| | ||||
| ||||
| |
| |||
| adam.timberlake***gmail.com escribió: > I was reading this article yesterday: > http://www.talkphp.com/advanced-php-...rray-item.html Yo también lo he leído pero he visto unos cuantos agujeros que no se han tenido en cuenta. Por ejemplo, en caso de que lleguen datos en $_POST en forma de array no será posible aplicarle la función htmlentities a cada elemento de la submatriz. > I am wondering.. okay.. we can use array_walk but doesn't that just > loop through the items anyway ?? So why not just use the foreach loop > instead... is it a shorthand trick or is there any deeper reasoning > behind it... Thats my question! Foreach te permite mucho más juego que array_walk (puedes hacer condicionales complejas y salidas del bucle con break, por ejemplo), al tiempo que array_walk es más rápido aplicando la función a cada elemento. > I do have another question and that is if it is a shorthand version, > having a foreach loop isn't much more in terms of the letters you type > and so why have the PHP developers put it in there ?? Ya te lo he dicho, está ahí porque no es lo mismo aplicar una función a cada elemento de una matriz que poder investigar en su interior, compararlo con el valor más alto, sumar los valores y un largo etcétera que no podría implementarse en funciones a no ser que se usaran variables globales o alguna solución similar para almacenar valores persistentes de una iteración a otra. > It's puzzling me. Thank you in advance. Espero que te haya aclarado un poco las ideas. Piensa este ejemplo: $anterior = 0; foreach ($matriz as $clave => $valor) { $salida .= $clave * $valor + $anterior; $anterior = $valor; } ¿Cómo implementarías eso con array_map o array_walk sin usar variables globales? Un saludo. -- Óscar Javier García Baudet LinaresDigital http://redstar.linaresdigital.com/ |
| |||
| adam.timberlake***gmail.com escribió: > I was reading this article yesterday: > http://www.talkphp.com/advanced-php-...rray-item.html Yo también lo he leído pero he visto unos cuantos agujeros que no se han tenido en cuenta. Por ejemplo, en caso de que lleguen datos en $_POST en forma de array no será posible aplicarle la función htmlentities a cada elemento de la submatriz. > I am wondering.. okay.. we can use array_walk but doesn't that just > loop through the items anyway ?? So why not just use the foreach loop > instead... is it a shorthand trick or is there any deeper reasoning > behind it... Thats my question! Foreach te permite mucho más juego que array_walk (puedes hacer condicionales complejas y salidas del bucle con break, por ejemplo), al tiempo que array_walk es más rápido aplicando la función a cada elemento. > I do have another question and that is if it is a shorthand version, > having a foreach loop isn't much more in terms of the letters you type > and so why have the PHP developers put it in there ?? Ya te lo he dicho, está ahí porque no es lo mismo aplicar una función a cada elemento de una matriz que poder investigar en su interior, compararlo con el valor más alto, sumar los valores y un largo etcétera que no podría implementarse en funciones a no ser que se usaran variables globales o alguna solución similar para almacenar valores persistentes de una iteración a otra. > It's puzzling me. Thank you in advance. Espero que te haya aclarado un poco las ideas. Piensa este ejemplo: $anterior = 0; foreach ($matriz as $clave => $valor) { $salida .= $clave * $valor + $anterior; $anterior = $valor; } ¿Cómo implementarías eso con array_map o array_walk sin usar variables globales? Un saludo. -- Óscar Javier García Baudet LinaresDigital http://redstar.linaresdigital.com/ |
| |
| |
![]() |
| Herramientas | |
| Desplegado | |
| |
Temas Similares | ||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| do while loop | Roberto | Newsgroup microsoft.public.es.excel | 4 | 15-07-2008 18:56:16 |
| LOOP REACTIVACION XP | juan toro | Newsgroup microsoft.public.es.windowsxp | 0 | 06-05-2008 15:58:03 |
| Why not use a foreach loop instead of having a function calledarray_walk? | adam.timberlake@gmail.com | Newsgroup es.comp.lenguajes.php | 0 | 08-01-2008 01:56:02 |
| foreach session | Segundo Serrano | Newsgroup microsoft.public.es.servicios.web | 2 | 03-05-2006 14:43:02 |
| foreach session | Segundo Serrano | Newsgroup microsoft.public.es.servicios.web | 0 | 24-04-2006 20:27:28 |