PHP Programing language

array_walk() and array_walk_recursive() Function in PHP
Previous Home Next

array_walk()

The array_walk() function runs each array element in a user-defined function. The array's keys and values are parameters in the function.

Syntax:
array_walk(array,myfunction,parameter...)

In above syntax, "array" specifying an array, "myfunction" it is the name of the user-defined function and "parameter,..." specifies a parameter to the user-defined function. You can assign one parameter to the function, or as many as you like

Example:

<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"Apple","b"=>"Mango","c"=>"Banana");
array_walk($a,"myfunction");
?>

Output:

The key a has the value Apple
The key b has the value Mango
The key c has the value Banana

 

array_walk_recursive()

The array_walk_recursive() function runs each array element in a user-defined function. The array's keys and values are parameters in the function. The difference between the array_walk() and the array_walk_recursive() function is that with this function you can work with deeper arrays (an array inside an array).

Syntax:
array_walk_recursive(array,myfunction,parameter...)

In above syntax, "array" specifying an array, "myfunction" it is the name of the user-defined function and "parameter,..." specifies a parameter to the user-defined function. You can assign one parameter to the function, or as many as you like

Example:

<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a1=array("a"=>"Apple","b"=>"Banana");
$a2=array($a1,"1"=>"Mango","2"=>"Orange");
array_walk_recursive($a2,"myfunction");
?>

Output:

The key a has the value Apple
The key b has the value Banana
The key 1 has the value Mango
The key 2 has the value Orange

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: