array_walk - for what does this good?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

array_walk - for what does this good?

Post by pelegk2 »

i read the array_walk help and didnt understand for what this is good?
does any 1 maybe have an idea?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_walk "walks" down an array, calling the supplied function with the details... It's pretty useful when you want to do differing special things to certain elements..
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

It is always a good idea to check the PHP Manual before posting questions. See how it's simple to find the reference for function array_walk: http://www.php.net/array_walk

Regards,
Scorphus.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I believe he did, but like he said, it's a bit confusing.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

i read the reference before i have asked
i just loked for intresting idea's
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

I use it like so:

Say you've fetched all the data from a MySQL query into an array (mysql_fetch_array) and want to stripslashes all of the elements in one go? Just do:

Code: Select all

function strip (&$item)
	{
		$item = stripslashes($item);
	}

...

	array_walk($data, 'strip');
But there are many other uses too.
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

That is a good idea launchcode, I was trying to figure out a good way of doing that, how simple! Glade you posted that question pelegk2! 8)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

There should be no need to stripslashes on data coming out of the database. You may addslashes to the data on the way in but when reteived the data will be returned unescaped without the need to stripslashes.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Depends on your magic quote settings.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

launchcode wrote:Depends on your magic quote settings.
No it doesn't. If you do not check for the magic quote settings and just addslashes regardless prior to inserting the info into the database then that is just poor coding. Effectively what you will have done is escaped the escapes, which is why you will have to then stripslashes from the data coming out of the database.

If you check and verify the data prior to inserting into the database and addslashes if nesseccary, then you will not have to stripslashes when retieving data from the database. If you have to stripslashes from the data being retrieved from the database then you are not storing the data correctly to begin with.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

I could name you a stack of very popular PHP applications that addslashes without a care for the world (or MQ GPC) where the above function has saved a lot of headaches. Like I said, there are many other uses for it array_walk which is what this thread is about, right?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

launchcode wrote:there are many other uses for it array_walk which is what this thread is about, right?
Yes, the thread is about the array_walk function, I am merley pointing out that there is something wrong if you have to stripslashes from data being retrieved from a database.
launchcode wrote:I could name you a stack of very popular PHP applications that addslashes without a care for the world (or MQ GPC)
Just because a stack of popular apps use it it doesn't make it right. I would class this approach as 'lazy coding' which when thinking about it probably makes it acceptable as PHP itself promotes lazy programming.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Just because a stack of popular apps use it it doesn't make it right.
I didn't say it was right, I just pointed out it happens - a lot. Whether you put that down to laziness on behalf of those developers or plain inexperience is up to you, and depends on which side your bread is buttered I guess.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I'm sure this thread could go on for sometime and achieve nothing. Easy solution or correct solution, I'll leave the decision up to you. Suffice to say, either way, my initial comment on this matter still stands... 'There should be no need to stripslashes on data coming out of the database'. Of course, that statement is based on the fact that the data within the database is/was stored correctly to begin with.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Post Reply