

PHP - array_map and callback functions
Author: DeVoid
callback functions of arrays are a very powerful instrument, using which, a programmer can obtain flexibility and simplicity of work with arrays. The callback functions of arrays are created for modification of content of arrays a method “value after a value”.
We will consider the function of array_map(), which in a general view looks so:
array array_map ( mixed $callback , array $array1 [, array $array2... ] )
the first parameter is a function of treatment of array, must work exactly with the that amount of parameters, which you will pass to it. For an example it is possible to consider simple example which will simply illustrate work of this function:
<?php
function format_values($value)
{
return '<b>'.$value.'</b>+';
}
$myarray = array('first', 'second', 'third');
$formatted_array = array_map("format_values", $myarray);
echo('<pre>');
print_r($formatted_array);
echo('</pre>');
?>
As a result of implementation of this code, we will get the values of our array in a bold style. As the first parameter of function of array_map we passed the name of function which will process every value of array $myarray. It should be noted that function of array_map return an array $formatted_array, every value of which is treated the function of format_values.
read comments (0)
