Ask a callback function question
Hello, I am a PHP beginner, when I look at the PHP array video, said the callback function, but not very understanding.
Can you explain it?
function Dump ($arr) {
echo "
";
Print_r ($arr);
}
$arr 1 = array ("url" = "bbs.houdunwang.com", "name" = "Backing net");
$arr 2 = Array ("Url2" = "bbs.houdunwang.com", "name" = "Backing net");
$arr 3 = Array ("weburl111" = "bbs.houdunwang.com");
$arr 0 = Array_intersect_uassoc ($arr 1, $arr 2,a);
Function A ($k 1, $k 2) {
if ($k 1 = = = $k 2) {
return 0;
}elseif ($k 1> $k 2) {
return 1;
}else{
return-1;
} }
Dump ($arr 0);
What do you mean by $K1 $k 2 in this code? callback function return value 0 1-1 What does it do to return to array judgments??
------Solution--------------------
The sorting algorithm can actually be abstracted into two parts:
1. The sorting algorithm itself, that is, how the elements in the order of comparison
2. Compare the rules, is to give you two elements, you have to tell me who the big who is small.
The usual sort is a direct comparison of numeric types, so direct use is greater than less than comparison.
The Uassoc family function provides a custom size comparison rule.
This kind of function realizes is the first part of the sorting algorithm, the second part is abstracted into a process, give you two elements, you tell me who big who small can.
For the example you gave, the two parameters that the a function receives are the two elements to compare.
The basic implementation of this type of callback is as follows:
(Example scenario: Find the element with the largest array value)
PHP Code
function Ud_max ($array, $callback) {if (empty ($array)) return false; $max = Next ($array); foreach ($array as $element) $max = $callback ($max, $element); return $max;} function Ud_compare1 ($max, $ele) {return ($max * $max > $ele * $ele)? $max: $ele;} function Ud_compare2 ($max, $ele) {return sqrt ($max) > sqrt ($ele)? $max: $ele;//root function name I'm not sure if it's sqrt.
------Solution--------------------
Easy to understand. The callback function is the function that writes itself normally does not call directly, to other function calls ... A little bit of a detour.
Like PHP's function Call_user_func_array
You can see the examples in the manual.
function Foobar ($arg, $arg 2) {
Echo __function__, "Got $arg and $arg 2\n";
}
class Foo {
function Bar ($arg, $arg 2) {
Echo __method__, "Got $arg and $arg 2\n";
}
}
Call the Foobar () function with 2 arguments
Call_user_func_array ("Foobar", Array ("One", "one"));
Call the $foo->bar () method with 2 arguments
$foo = new Foo;
Call_user_func_array (Array ($foo, "bar"), Array ("Three", "four");
?>
------Solution--------------------
A callback function is a function called by a function pointer, which means that the callback function is called by another function as a parameter.
What do you mean by $K1 $k 2 in this code? callback function return value 0 1-1 What does it do to return to array judgments??
For your code, the A () function is the callback function that returns the two value-size relationship.
$k 1, $k 2 is the formal parameter of the function.
The return value of the callback function acts as a sort rule for the function calling the function (that is, you need the sort function to sort the rules)