Home >>PHP Array Functions >PHP array_walk() Function

PHP array_walk() Function

PHP array_walk() Function

PHP array_walk() function is used to run each elements of the given input array in to a user-defined function. The keys and the values of the array are used as the parameter for the user-defined function. It returns a Boolean value TRUE on success and False on failure.

Syntax:

array_walk(array, function, parameter...);

Parameter Values

Parameter Description
array This is a requiredparameter.This parameter definesthe given array.
function This is a requiredparameter.This parameter definesthe name of the user-defined function.
parameter,... This is an optionalparameter.This parameter defines a parameter to the user-defined function.

Here is an example of array_walk() function in PHP:

<html>
<body>
<?php
function myfunction($value,$key)
{
echo "$key work as a $value.<br>";
}
$x=array("Jerry"=>"Developer","Rohit"=>"Designer","Kundan"=>"Marketing Head","Shubham"=>"Manager");
array_walk($x,"myfunction");
?>
</body>
</html>
Output:
Jerry work as a Developer.
Rohit work as a Designer.
Kundan work as a Marketing Head.
Shubham work as a Manager.

Example 2:

<html>
<body>
<?php
function myfunction($value,$key,$p)
{
echo "$key $p $value.<br>";
}
$a=array("Abhi"=>"Lamborghini","Kundan"=>"BMW","Gautam"=>"Jaguar","Ashish"=>"Audi","Kriti"=>"Ferrari");
array_walk($a,"myfunction","owns a");
?>
</body>
</html>
Output:
Abhi owns a Lamborghini.
Kundan owns a BMW.
Gautam owns a Jaguar.
Ashish owns a Audi.
Kriti owns a Ferrari.

No Sidebar ads