array_map to apply function to elements of array
my_function : Required , user defined or native PHP functions array1 : Required , All elements has to pass through the function array2 : Optional , All elements has to pass through the function Output is an array ( return value of function ) Here number of arrays must be equal to number of parameters passed to array_map function. We will try some examples. We need to reduce the value of all the elements of an array by half. Here we can easily use array_map function and change.
The output of above code is here
Let us add elements of two arrays , note that both arrays have equal number of elements
The output is here
We can use PHP native function also. Here is an example where we used string function strtoupper to change all lower case elements to upper case elements of an array.
The output is here
We will use one user defined function array_add to add two each elements of two arrays. We have used strings here as elements.
The output of above code is here.
Difference between array_map and array_walk
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
![]() | |||||||||||||||
| |||||||||||||||