Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. array_reduce() / array_walk()

array_reduce() / array_walk() Since: PHP 4(2000)

Functions that reduce all array elements into a single value, or apply a callback to each element in sequence.

Syntax

// Processes each element of the array with a callback and reduces them to a single value.
array_reduce(array, callback, initial_value);

// Applies a callback to each element of the array. The original array is modified in place.
array_walk(array, callback, extra_data);

Function List

FunctionDescription
array_reduce($array, $callback, $initial)Processes each element from the beginning of the array and reduces them to a single value. The callback receives the accumulated result so far and the current element.
array_walk($array, $callback, $arg)Applies a callback to each element of the array. The first argument of the callback is passed by reference, allowing you to modify the original array directly.

Sample Code

<?php
// Calculate the sum of an array.
$prices = [150, 80, 300, 200];
$total = array_reduce($prices, fn($carry, $item) => $carry + $item, 0);
echo $total; // Outputs "730".

// Find the maximum value.
$scores = [85, 92, 78, 95, 88];
$max = array_reduce($scores, fn($carry, $item) => max($carry, $item), 0);
echo $max; // Outputs "95".

// Concatenate array elements into a string.
$words = ['PHP', 'is', 'fun'];
$sentence = array_reduce($words, fn($carry, $item) => $carry . $item, '');
echo $sentence; // Outputs "PHPisfun".

// Convert an associative array to HTML option tags.
$options = ['jp' => 'Japan', 'us' => 'United States', 'uk' => 'United Kingdom'];
$html = array_reduce(
    array_keys($options),
    fn($carry, $key) => $carry . "\n",
    ''
);
echo $html; // Outputs HTML option tags.

// Use array_walk to modify each element of an array.
$items = ['apple', 'orange', 'grape'];
array_walk($items, function(&$value, $key) {
    $value = ($key + 1) . '. ' . $value; // Prepend a number to each item.
});
print_r($items); // Each element is now numbered.

// Use array_walk to process an associative array.
$prices = ['apple' => 150, 'orange' => 80, 'grape' => 300];
array_walk($prices, function(&$price, $name, $tax_rate) {
    $price = (int)($price * $tax_rate); // Convert to tax-inclusive price.
}, 1.1);
print_r($prices); // Prices are updated to include tax.

Details

array_reduce() combines all elements of an array into a single value. The callback receives the result of the previous call as its first argument and the current element as its second. The third argument sets the initial value used for the first call; if omitted, it defaults to null.

array_walk() applies a callback to each element of the array. If you pass the first argument of the callback by reference (&$value), you can modify the original array directly. You can also pass extra data as the third argument, which is useful when you need a shared parameter — such as a tax rate — across all elements.

For simple totals or element counts, array_sum() / count() is more concise. To transform each element and produce a new array, use array_map() instead.

If you find any errors or copyright issues, please .