Re: array_walk?

From: Kelvin Mackay (kelvin_at_nospam.sands-design.net)
Date: 02/20/05


Date: Sun, 20 Feb 2005 12:05:39 -0000

On 18 Feb 2005 11:30:30 -0800, comp.lang.php <phillip.s.powell@gmail.com>
wrote:

> I'm beginning to wonder myself since array_walk() has more limitations
> than us the foreach loop construct inasmuch as direct array
> manipulation is concerned.
>
> You don't have to reset the pointer before array_walk() whereas in
> foreach you would have to before using it to ensure the looping entails
> the entire array; that's the only advantage I can think off offhand.

Nope - www.php.net/foreach

"Note: When foreach first starts executing, the internal array pointer is
automatically reset to the first element of the array. This means that you
do not need to call reset() before a foreach loop."

>
> Phil
>

Presumeably the merit of array_walk is in avoiding code duplication where
you might otherwise have to use several identical foreach blocks

--
Kelvin


Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: App Caused Pocket PC to Soft Reset on its Own?
    ... I don't know of any specific error that is likely to cause a reset. ... problem code could have existed without showing this symptom. ... Check each pointer before you use it. ... Check array bounds. ...
    (microsoft.public.pocketpc.developer)
  • Re: array_walk?
    ... > foreach you would have to before using it to ensure the looping entails ... > the entire array; that's the only advantage I can think off offhand. ... When foreach first starts executing, the internal array pointer is ... automatically reset to the first element of the array. ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... build an array of keys I want to delete, and after the loop ends, delete ... And until I see something from the PHP developers saying it is OK, ... foreach operates on a copy of the ... Therefore, the array pointer ...
    (comp.lang.php)
  • Re: [PHP] foreach
    ... I thought foreachalready performed a reset()? ... Well, corn my fritters, according to TFM, it does this indeed. ... Actually, foreach() creates a copy of the mentioned array, and the array ...
    (php.general)