Home > Backend Development > PHP Tutorial > How do array_map, array_walk, and array_filter differ in their behavior and usage in PHP?

How do array_map, array_walk, and array_filter differ in their behavior and usage in PHP?

DDD
Release: 2024-11-03 13:06:03
Original
1024 people have browsed it

How do array_map, array_walk, and array_filter differ in their behavior and usage in PHP?

Distinguishing array_map, array_walk and array_filter in PHP

Although array_map, array_walk, and array_filter are all PHP functions that take a callback function as an argument to perform an action on an array, there are subtle differences in their behavior and usage.

Key Differences:

1. Value Modification:
array_map cannot modify the values within the input array, while array_walk can. This is because array_map creates a new array with the results of the callback function, while array_walk modifies the input array in place.

2. Array Key Access:
array_map cannot operate on array keys, while array_walk can. This means that array_walk can perform operations based on both the values and keys of the array.

3. Return Value:
array_map returns a new array containing the results of the callback function, while array_walk returns true if the callback function was successfully applied to all elements of the input array.

4. Multiple Array Iteration:
array_map can accept multiple arrays as input and iterate over them in parallel, allowing for operations on multiple arrays at once. array_walk, on the other hand, operates on only one array.

5. Callback Argument Passing:
array_walk allows for an additional arbitrary parameter to be passed to the callback function, providing flexibility in the actions performed within the callback.

6. Returned Array Length:
array_map produces a result array with the same length as the largest input array. array_walk does not return an array and cannot alter the number of elements in the input array. array_filter returns a subset of the original array based on the filtering function and preserves the original keys.

Example:

Consider the following code:

<code class="php">$a1 = array(2.4, 2.6, 3.5);
$a2 = array(2.4, 2.6, 3.5);

array_map('floor', $a1); // $a1 remains unchanged
array_walk($a2, function (&$v) { $v = floor($v); }); // modifies $a2</code>
Copy after login

In the first case, array_map creates a new array with the floor values of each element in $a1, but $a1 itself remains unmodified. In the second case, array_walk directly modifies the elements of $a2 by rounding them down to the nearest integer.

The above is the detailed content of How do array_map, array_walk, and array_filter differ in their behavior and usage in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template