array_map:
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
array_map — Applies the callback to the elements of the given arrays
Description:
[ad type=”banner”]array_map() returns an array containing all the elements of array1 after applying the callback function to each one.
The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()
Example #1
array_map() example:
This makes $b have:
array_filter
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
array_filter — Filters elements of an array using a callback function
Description
Iterates over each value in the array passing them to the callback function.
If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
Example #2
array_filter() without callback
[ad type=”banner”]The above example will output:
array_walk
(PHP 4, PHP 5, PHP 7)
array_walk — Apply a user supplied function to every member of an array
Description :
- Applies the user-defined callback function to each element of the array array.
- array_walk() is not affected by the internal array pointer of array.
- array_walk() will walk through the entire array regardless of pointer position.
Example #1
array_walk() example
[ad type=”banner”]The idea of mapping an function to array of data comes from functional programming.
- You shouldn’t think about array_map as a foreach loop that calls a function on each element of the array.
- It should be thought of as applying the function to each element in the array independently.
In theory such things as function mapping can be done in parallel since the function being applied to the data should ONLY effect the data and NOT the global state.
- This is because an array_map could choose any order in which to apply the function to the items in (even though in PHP it doesn’t).
array_walk on the other hand it the exact opposite approach to handling arrays of data.
- Instead of handling each item separately, it uses a state (&$userdata) and can edit the item in place (much like a foreach loop).
- Since each time an item has the $funcname applied to it, it could change the global state of the program and therefor requires a single correct way of processing the items.
Back in PHP land, array_map and array_walk are almost identical except array_walk gives you more control over the iteration of data and is normally used to “change” the data in-place vs returning a new “changed” array.
array_filter is really an application of array_walk (or array_reduce) and it more-or-less just provided for convenience.
easy
nice article… keep posting wow
Gud article
wow