PHP array_walk() Function

array_walk is one of the powerful array function , which is working based on user defined function as an argument. Here is the small example below.

 function add_string($val,$key,$par) {
 echo $val." ".$par. " <br />";
 echo "Hello, Good Morning <br />";
 $a    =    array("Milap","Jay","Parthiv","Sandip","Smit");
 array_walk($a,'add_string','Loves PHP');
Output :-

Hello, Good Morning
Milap Loves PHP
Jay Loves PHP
Parthiv Loves PHP
Sandip Loves PHP
Smit Loves PHP

Small Explanation :-

add_string function is passed as an argument in array_walk function, it means, the logic of add_string will apply to each element of array. 3rd parameter is optional.


One thought on “PHP array_walk() Function

I like to hear from you about this !!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.