For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2005 > array_walk?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author array_walk?
steve

2005-02-18, 3:56 pm

Hi, I have never used array_walk, and quite frankly, cannot see why I
should use it instead of foreach.

Can someone shed some light on the cases where array_walk is the one
to use, and what php developers where thinking when they coded this
operator.

Thanks.

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-array_w...pict199368.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=676653
Matt Mitchell

2005-02-18, 3:56 pm


"steve" <UseLinkToEmail@dbForumz.com> wrote in message
news:42163460$1_5@alt.athenanews.com...
> Hi, I have never used array_walk, and quite frankly, cannot see why I
> should use it instead of foreach.
>
> Can someone shed some light on the cases where array_walk is the one
> to use, and what php developers where thinking when they coded this
> operator.
>


array_walk() has been there as a function since v3.0.3, foreach as a
language construct has been there since v4.

the new array_walk_recursive() looks like a handy function though...

Matt


comp.lang.php

2005-02-18, 3:56 pm

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.

Phil

Wayne

2005-02-18, 8:55 pm

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

>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.


You don't have to reset() before a foreach...

Chung Leong

2005-02-19, 3:56 am

"steve" <UseLinkToEmail@dbForumz.com> wrote in message
news:42163460$1_5@alt.athenanews.com...
> Hi, I have never used array_walk, and quite frankly, cannot see why I
> should use it instead of foreach.
>
> Can someone shed some light on the cases where array_walk is the one
> to use, and what php developers where thinking when they coded this
> operator.


array_walk() should be a little faster than using a foreach() loop, as it's
a built-in function. Isn't terribly useful because it needs a reference. I
use array_map() much more often:

// trimming an array
$a = array_map('trim', $a);

// filename-only of results from glob()
$f = array_map('basename', glob("*.gif"));


Kelvin Mackay

2005-02-20, 8:55 am

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
Fletch

2005-02-22, 8:56 pm

steve wrote:

> Hi, I have never used array_walk, and quite frankly, cannot see why I
> should use it instead of foreach.
>
> Can someone shed some light on the cases where array_walk is the one
> to use, and what php developers where thinking when they coded this
> operator.
>
> Thanks.
>

From php.net/foreach
Note: Also note that foreach operates on a copy of the specified array and
not the array itself. Therefore, the array pointer is not modified as with
the each() construct, and changes to the array element returned are not
reflected in the original array. However, the internal pointer of the
original array is advanced with the processing of the array. Assuming the
foreach loop runs to completion, the array's internal pointer will be at
the end of the array.

So foreach makes a copy, and nested foreach's make more copies. Sometimes
this is very bad.

I mostly use while(list($xKey,$xVal)=each($aArray)) for string indexed
arrays and $iCount=count($aArray) for($i=0;$i<$iCount;$i++) for number
indexed arrays. Can't always just use these though :(

As per Zend PHP certification book :)
--
Fletch
A: Because it disrupts the natural flow when reading
Q: Why is it bad to top post?
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com