PHP recursively calls the array value and uses it to execute the specified function _ PHP Tutorial

Source: Internet
Author: User
PHP recursively calls the array value and uses it to execute the specified function. PHP recursively calls the array value and uses it to execute the specified function. The following is the original wordpress code. to be lazy, simply modify it to apply to other functions. 12345678910111213141 PHP recursively calls the array value and uses it to execute the specified function.

The following is the original wordpress code. to be lazy, modify it to apply other functions.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

/**

* Navigates through an array and removes slashes from the values.

*

* If an array is passed, the array_map () function causes a callback to pass

* Value back to the function. The slashes from this value will removed.

*

* @ Since 2.0.0

*

* @ Param mixed $ value The value to be stripped.

* @ Return mixed Stripped value.

*/

Function stripslashes_deep ($ value ){

If (is_array ($ value )){

$ Value = array_map ('stripslashes _ deep ', $ value );

} Elseif (is_object ($ value )){

$ Vars = get_object_vars ($ value );

Foreach ($ vars as $ key => $ data ){

$ Value-> {$ key} = stripslashes_deep ($ data );

}

} Elseif (is_string ($ value )){

$ Value = stripslashes ($ value );

}

Return $ value;

}

The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

Function function_deep ($ function, $ value ){

Try {

If (! Function_exists ($ function )){

$ Error = '"'. $ function. '" is undefined ';

Throw new Exception ($ error );

}

} Catch (Exception $ e ){

Echo 'caught exception: ', $ e-> getMessage (), "\ n ";

Die ();

}

If (is_array ($ value )){

$ Fun = Array ();

For ($ I = 1; $ I <= count ($ value); $ I ++ ){

$ Fun [] = $ function;

}

$ Value = array_map ("function_deep", $ fun, $ value );

} Elseif (is_object ($ value )){

$ Vars = get_object_vars ($ value );

Foreach ($ vars as $ key => $ data ){

$ Value-> {$ key} = function_deep ($ function, $ data );

}

} Elseif (is_string ($ value )){

$ Value = call_user_func ($ function, $ value );

}

Return $ value;

}

$ Arr = array (

"I'm bean ",

"I'm bean ",

Array ("I'm bean", "I'm bean ")

);

Var_dump (function_deep ("addslashes", $ arr ));

// Output result

// Array (size = 3)

// 0 => string 'I \'m Bean' (length = 9)

// 1 => string 'I \'m Bean' (length = 9)

// 2 =>

// Array (size = 2)

// 0 => string 'I \'m Bean' (length = 9)

// 1 => string 'I \'m Bean' (length = 9)

?>

The following is the original wordpress code. to be lazy, simply modify it to apply other functions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1...

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.