R4R Team working to provide free of cost for experienced and freshers

Tutorials | Examples|Interview Questions and Answers|Java Tutorials| C Tutorials | C++ Tutorials | Spring Tutorials| Hibernate Tutorials | JSP Tutorials | Servelets Tutorials| C# Tutorials | PHP Tutorials etc , Basics | Download Software | Data Types | Variables | Constant | Storage & Classes | Operators | Decisions Making | Loops | Functions| Numbers | Arrays | Pointers | String | Datastructure | Expressions | Preprocessor Statement | I/O | Exception Handling | OOPs Tutorial | Classes and object |C++ Inheritance Overloading | Polymorphism | Abstraction | Encapsulation | Interfaces | Multithreading etc

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