473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

array_walk?

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_wa...ict199368.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=676653
Jul 17 '05 #1
6 3038

"steve" <Us************@dbForumz.com> wrote in message
news:42**********@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
Jul 17 '05 #2
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

Jul 17 '05 #3
On 18 Feb 2005 11:30:30 -0800, "comp.lang.php"
<ph**************@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...

Jul 17 '05 #4
"steve" <Us************@dbForumz.com> wrote in message
news:42**********@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"));
Jul 17 '05 #5
On 18 Feb 2005 11:30:30 -0800, comp.lang.php <ph**************@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
Jul 17 '05 #6
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?
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
<?php class Grad { var $dbFormExemptionArray = array(); function Grad ($id = '') { ...
2
by: Reply-Via-Newsgroup | last post by:
Folks, I have a multi-dimensional array that I read from my mysql database. I'd like to run strip slashes against each element and I'm pretty sure that array_walk() (or array_map) is likely to...
8
by: Craig Thomson | last post by:
I was wondering what people do with text provided by the user in a form. Some cleaning needs to be done at some stage if you are going to be putting it in a database or displaying it etc. But when...
1
by: comp.lang.php | last post by:
/** * Filter results according to given instruction * * @access private * @param object $result (reference) * @return object $filteredResult */ function &filterResults(&$result) { //...
2
by: lwoods | last post by:
I have the following function: function clean_form( &$from_check ) { if(is_array($from_check)){ array_walk(&$from_check,'clean_form'); return; } else { $value =...
56
by: tasteless | last post by:
Hi guys, I need really hard questions (about 10) about PHP programming (some of elements OOP as well, but no MySQL questions - this is different part), this questions needs to be very hard, but...
12
by: Steve | last post by:
here's a quirk i can't seem to handle, just hack. since call-time by-reference is depreciated and i don't want to enable it in the php.ini, i'm kind of stuck when i want to pass userdata as an...
1
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to...
0
code green
by: code green | last post by:
Using the third parameter to pass the address of a user array to array_walk fills the user array with data. But once array_walk finishes executing the user array empties. Can any body explain this?...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.