Question about the callback function

Source: Internet
Author: User
I am a beginner in PHP. I spoke about the callback function while watching the PHP array video. Can you explain this? Functiondump ($ arr) {& nbsp; echo & quot; & lt; pre & gt; & quot; & nb ask the callback function question
Hello everyone, I am a beginner in PHP. I spoke about the callback function while watching the PHP array video, but I am not very familiar with it.

Can you explain this?

Function dump ($ arr ){
Echo"
";
Print_r ($ arr );
}
$ Arr1 = array ("url" => "bbs.houdunwang.com", "name" => "");
$ Arr2 = array ("url2" => "bbs.houdunwang.com", "name" => "");
$ Arr3 = array ("weburl111" => "bbs.houdunwang.com ");
$ Arr0 = array_intersect_uassoc ($ arr1, $ arr2, );
Function a ($ k1, $ k2 ){
If ($ k1 ===$ k2 ){
Return 0;
} Elseif ($ k1> $ k2 ){
Return 1;
} Else {
Return-1;
}}
Dump ($ arr0 );

In this code, what does $ k1 $ k2 mean? The return value of the callback function is 0 1-1. how does the return value play a role in array determination ??

------ Solution --------------------
The sorting algorithm can be abstracted into two parts:
1. sorting algorithm itself, that is, the order in which elements are compared
2. the comparison rule is to give you two elements. you need to tell me who is big and who is small ..

Generally, the sorting is a direct comparison of the numeric type. Therefore, you can directly use a value greater than or less than the comparison.

Uassoc functions provide custom size comparison rules.
This type of function implements the first part of the sorting algorithm. it abstracts the second part into a process and gives you two elements. you can tell me who is big or small.

For your example, the two parameters received by function a are the two elements used for comparison.

The basic implementation of such 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; // The name of the open function. I'm not sure if it's sqrt}
------ Solution --------------------
To put it simply, the callback function is a self-written function that is generally not directly called by itself. it is a bit difficult to call other functions...

For example, the PHP function call_user_func_array

You can see the example in the manual.
Function foobar ($ arg, $ arg2 ){
Echo _ FUNCTION __, "got $ arg and $ arg2 \ n ";
}
Class foo {
Function bar ($ arg, $ arg2 ){
Echo _ METHOD __, "got $ arg and $ arg2 \ n ";
}
}


// Call the foobar () function with 2 arguments
Call_user_func_array ("foobar", array ("one", "two "));

// 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. that is to say, a callback function is called by another function as a parameter.


// What does $ k1 $ k2 mean in this code? The return value of the callback function is 0 1-1. how does the return value play a role in array determination ??
For your code, the () function is the callback function, and the two values returned are in the size relationship.
$ K1, $ k2 is the form parameter of the function.
The return value of the callback function serves as a sorting rule for the function that calls this function (that is, the sort function is used to sort the rules)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.