473,789 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3051

"steve" <Us************ @dbForumz.com> wrote in message
news:42******** **@alt.athenane ws.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_recu rsive() 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.athenane ws.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('base name', 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($xKe y,$xVal)=each($ aArray)) for string indexed
arrays and $iCount=count($ aArray) for($i=0;$i<$iC ount;$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
1967
by: Phil Powell | last post by:
<?php class Grad { var $dbFormExemptionArray = array(); function Grad ($id = '') { /*---------------------------------------------------------------------------------------------------------------------- Do note that if you are generating arrays that will not have
2
2960
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 solve my problem - But I've not got the foggiest on how to use it - I've taken a look in the php.chm manual supplied from php.net and I don't seem to have had any success so far - Can anyone suggest how I could stripslashes to every element...
8
4992
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 is the time to do that? Do you clean it as soon as you get it? Do you pass around the original text and clean it when you use it? What about magic slashes? You need to addslashes before using in a db statement, but you need to strip them...
1
1822
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) { // STATIC OBJECT ARRAY METHOD global $section;
2
2177
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 = str_replace(array("\r","\n","Content-Type:"),"",$from_check); }
56
2992
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 the experienced senior PHP developer should answered on it. I've already searched in google and google groups archive but without any good results. So could anybody help me giving some link or sending some stuff to me ?
12
2959
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 array byref that is initially = array(). // the array being walked $numbers = array(1, 56, 999, 1000, 28, 65); // the work-around
1
2825
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 enter input. I need to take this user input and put it back into the database. What would be the best method to do this. I can't use a normal post because the name of the text box is the same for each table row. I've heard that posting the...
0
1009
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? $assocPriceLists = array(); //Create price lists lookup table if(array_walk($priceLists, 'createAssoc', $assocPriceLists)) { print_r($assocPriceLists); //Prints Array(). assocPriceLists is empty function...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8998
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4078
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.