Example 3. Using anonymous functions as
callback functions
<?php
$av = array("the ", "a ", "that
", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v .
"mango";'));
print_r($av);
?>
outputs:
Array
(
[0] => the mango
[1] => a mango
[2] => that mango
[3] => this mango
)
an array of strings ordered from shorter to longer
<?php
$sv = array("small", "larger", "a
big string", "it is a string thing");
print_r($sv);
?>
outputs:
Array
(
[0] => small
[1] => larger
[2] => a big string
[3] => it is a string thing
)
sort it from longer to shorter
<?php
usort($sv, create_function('$a,$b','return strlen($b) -
strlen($a);'));
print_r($sv);
?>
outputs:
Array
(
[0] => it is a string thing
[1] => a big string
[2] => larger
[3] => small
)
|