PHP array_map() Function

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.

Tip: You can assign one array to the function, or as many as you like.

ParameterDescription
myfunctionRequired. The name of the user-made function, or null
array1Required. Specifies an array
array2Optional. Specifies an array
array3Optional. Specifies an array

Syntax

 array_map(myfunction,array1,array2,array3...) 

Example

function myfunction($name)
{
	if ($name==="Alex")
	{
		return "Lachlan";
	}
	return $name;
}

$array=array("Cooper","Samuel","Alex");
print_r(array_map("myfunction",$array));

Output

Array ( [0] => Cooper [1] => Samuel [2] => Lachlan ) 

Leave a Reply

Your email address will not be published. Required fields are marked *