Page 1 of 1

array_walk - for what does this good?

Posted: Sun Jul 04, 2004 10:36 am
by pelegk2
i read the array_walk help and didnt understand for what this is good?
does any 1 maybe have an idea?

Posted: Sun Jul 04, 2004 10:44 am
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..

Posted: Sun Jul 04, 2004 11:15 am
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.

Posted: Sun Jul 04, 2004 11:38 am
by d3ad1ysp0rk
I believe he did, but like he said, it's a bit confusing.

Posted: Mon Jul 05, 2004 1:39 am
by pelegk2
i read the reference before i have asked
i just loked for intresting idea's

Posted: Mon Jul 05, 2004 7:17 am
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.

Posted: Mon Jul 05, 2004 7:57 am
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)

Posted: Mon Jul 05, 2004 8:11 am
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.

Posted: Mon Jul 05, 2004 8:19 am
by launchcode
Depends on your magic quote settings.

Posted: Mon Jul 05, 2004 8:28 am
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.

Posted: Mon Jul 05, 2004 8:43 am
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?

Posted: Mon Jul 05, 2004 8:59 am
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.

Posted: Mon Jul 05, 2004 9:26 am
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.

Posted: Mon Jul 05, 2004 10:15 am
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.

Posted: Mon Jul 05, 2004 2:49 pm
by McGruff