Newsgrupos.com  

Retroceder   Newsgrupos.com > Forum > Newsgroup es.comp.lenguajes.* Foro > Newsgroup es.comp.lenguajes.php
Registrarse Preguntas Frecuentes Lista de Foreros Calendario Buscar Temas de Hoy Marcar Foros Como Leídos




Respuesta
 
LinkBack Herramientas Desplegado
  #1 (permalink)  
Antiguo 08-01-2008, 01:56:02
adam.timberlake@gmail.com
 
Mensajes: n/a
Predeterminado Why not use a foreach loop instead of having a function calledarray_walk?

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.
Responder Con Cita
Alt Today
Advertising
Google Adsense
 
This advertising will not be shown
in this way to registered members.
Register your free account today
and become a member on
Newsgrupos.com
Standard Sponsored Links

  #2 (permalink)  
Antiguo 09-01-2008, 21:39:32
Oscar Garcia
 
Mensajes: n/a
Predeterminado Re: Why not use a foreach loop instead of having a function calledarray_walk?

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/
Responder Con Cita
  #3 (permalink)  
Antiguo 09-01-2008, 21:39:32
Oscar Garcia
 
Mensajes: n/a
Predeterminado Re: Why not use a foreach loop instead of having a function calledarray_walk?

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/
Responder Con Cita
 
Respuesta


Herramientas
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Trackbacks are habilitado
Pingbacks are habilitado
Refbacks are habilitado


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





Powered by: vBulletin, Versión 3.6.8
Derechos de Autor ©2000 - 2008, Jelsoft Enterprises Ltd.

LinkBacks Enabled by vBSEO 3.1.0 © 2007, Crawlability, Inc.