Array Map Function in PHP

In this article I explain array map function in PHP.
  • 1503

PHP array_map function

The array_map function sends each value of an array to the user-defined function and returns an array containing all the elements of arr1 after applying the user-defined function to each one.

Syntax

array_map( userDefinedFunction, array1, array2,........................)

Parameters

The parameters of the array_map function are:

Parameter Description
userDefinedFunction It specifies name of user defined function.
array1 It specifies an array.
array2 It  specifies an array.

Example

An example of the array_map function is:

<?php
function
 myfunction($array1)
{

if
 ($array1==="Mango")
{

return
 "orange";
}

return
 $array1;
}
$
array1 = array(1 => "Apple",  2=> "Mango", 3 => "Orange", "Banana");
echo "
<pre>";
$result = array_map("myfunction",$array1);
print_r($result);

?>

Output

array-map-function-in-php.jpg  

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.