The old rules, first code before talking.
<?php function Showarray ($cols) { foreach ($cols as $obj) { echo $obj.
" ";
} echo "<br>"; The function showobj ($obj) { echo $obj.
" ";
}
Mswap passed in a reference type.
Function Mswap (& $a,& $b) { $tmp = $a; $a = $b; $b = $tmp;} Function Bubblesort (& $cols) { $len =count ($cols) for ($i =0; $i < $len; $i + +) { F
or ($j =1 $j < $len-$i $j + +) { if ($cols [$j -1]> $cols [$j]) {
Mswap ($cols [$j-1], $cols [$j]);
} } } $data = Array (8,2,3,9,0,45,35,235);
Sort Bubblesort ($data);
Print array Showarray ($DATA);
Use the callback function to iterate through the print array array_map (' Showobj ', $data);
echo "<br>"; Use anonymous functions to traverse the print array array_map create_function (' $obj ', ' echo $obj. "
"; '), $data);
echo "<br>";
Use the new method (php5.3+) to create an anonymous function Array_map (function ($obj) {echo $obj. "";}, $data); anonymous function captures external variable $msg ="Val:"; Array_map (function ($obj) use ($msg) {echo $msg. $obj. "
";}, $data);
?>
The output of the program:
The function of the code is very simple, first initialize an array, then sort it, and finally print it out in three different ways.
The sorting algorithm uses the simplest bubble sort and does not repeat.
To be aware of are:
(1) Both the sorting function and the Exchange function use the method of passing the reference. The sorting and swapping functions do not take effect if the value is used, because at this point the function actually operates a copy of the Parameter object.
(2) The Array_map is PHP-led, which is the function of each object in the array to call the incoming function in turn. This approach is called a callback.
(3) Create_function can create an anonymous function. In the last print example, it is the anonymous function that it creates that is used as a array_map parameter.
In fact, experienced programmers know that anonymous functions are inefficient in both c++11 and PHP. So why use anonymous functions? Why are the new standards in C # and Java supporting it?
Because a well-known function may seem intuitive, there is a cost of management. One more function, one more place to manage. And for a "miniature" piece of code, writing a well-known function for it alone also appears to be less tightly coded. This does not mean, however, that anonymous functions can be misused, and that using anonymous functions can severely damage readability when code fragments are too large. The use of anonymous functions depends on the situation.
The problem with Create_function is that the code it creates for the anonymous function to execute is placed in a string, and readability is not very good.
Fortunately, there is a new function in php5.3+ to create anonymous functions. To see how to use (still walk through an array with anonymous functions).
Use the new method (php5.3+) to create an anonymous function
array_map (function ($obj) {echo $obj. "";}, $data);
is not much more elegant. →_→ ...
Another problem: Now that the anonymous function can only use its own arguments, what if it needs to use variables outside of its scope? The USE keyword in PHP provides support for this:
An anonymous function captures an external variable
$msg = "val:";
Array_map (function ($obj) use ($msg) {echo $msg. $obj. " ";}, $data);
The output at this point is:
val:0 val:2 val:3 val:8 val:9 val:35 val:45 val:235
In addition, the ARRAY_MAP array in this example has only been traversed, and the callback function does not change the elements in the arrays. If the callback function changes the contents of the array, it needs to be received by the return value.
There is another array_walk function in PHP that functions like Array_map, but when the callback function needs to change the data in the array, it is done by passing a reference to the array.
Take a look at the example:
<?php
function F1 ($n)
{
$n *=;
return $n;
}
The F2 parameter is a reference type
function F2 (& $n)
{
$n/=
}
$data = Array (
1,
2,
3,
4,
5
);
Array_map receives a modified array by returning a value
$data = Array_map ("F1", $data);
Var_dump ($data);
Array_walk changes the value of an array by passing a reference
Array_walk ($data, "F2");
echo "<br>";
Var_dump ($data);
? >
The code is also simple, using Array_map to call the callback F1 by multiplying each value in the array by 100 and then using Array_walk to call the callback F2 in turn, dividing each value in the array by 100.
If you do not want the callback function to change the value of the array, F1 instead of returning the value, F2 instead of passing the reference.