1. array_map()
Purpose: Applies a callback function to each element of one or more arrays and returns a new array with the results.
Key Points:
Returns a new array.
Does not modify the original array.
Can work with multiple arrays in parallel.
2. array_filter()
Purpose: Filters an array using a callback function — keeps only the values for which the callback returns true.
Key Points:
Returns a new filtered array.
Does not modify the original array.
Removes elements that don’t match the condition.
3. array_walk()
Key Points:
Modifies the original array.
Does not return anything useful (returns true on success).
Cannot change keys.
Purpose: Applies a callback function to each element of the array by reference. Mostly used for modifying the array in place.
Summary:
Function | Returns New Array? | Modifies Original? | Purpose |
---|---|---|---|
array_map() |
Yes | No | Transform values |
array_filter() |
Yes | No | Filter values |
array_walk() |
No | Yes | Modify values (by reference) |