Skip to content

PHP array_map() with multiple callbacks?

We can pass multiple Callback function to array_map with use of anonymous function , just pass the anonymous function as arguments to array_map() function , Anonymous functions are available from PHP 5.3 .See the below example for better understanding.

$array = array('arjun ',"php");
$result=array_map(function($item){ return strtoupper(trim($item)); }, $array);
print_r($result);

Using PHP’s create_function();

$array = array('arjun ',"php");
$result =array_map(create_function('$item', 'return strtoupper(trim($item));'),$array);
print_r($result);

nesting array_map() calls

$array = array('arjun','php');
$result = array_map('strtoupper', array_map('trim', $array));
print_r($result);

PHP Functions Reference

array() : Create an array

array_map(callback, arr1): Applies the callback to the elements of the given arrays

create_function(args, code) : create an anonymous function

function() { } : Anonymous function

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments