How to use array_map function in php
Editor to share with you how to use the array_map function in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Array_map acts a user-defined function on each value of a given array, returning a new value.
This function is somewhat similar to array_walk_recursive, except that it writes one more step of recursion.
Function myfunction ($v) {if (is_array ($v)) {return array_map ("myfunction", $v);} return ($v * $v);} $a = array (1,2,3,4,5,6 = > [2,3]); print_r (array_map ("myfunction", $a))
Output:
Array
(
[0] = > 1
[1] = > 4
[2] = > 9
[3] = > 16
[4] = > 25
[6] = > Array
(
[0] = > 4
[1] = > 9
)
)
Function myfunction ($v1, $v2) {if ($v1 = = $v2) {return "same";} return "different";} $A1 = array ("Horse", "Dog", "Cat"); $a2 = array ("Cow", "Dog", "Rat"); print_r (array_map ("myfunction", $A1, $a2))
Output:
Array
(
[0] = > different
[1] = > same
[2] = > different
The above is all the contents of the article "how to use the array_map function in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!