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_map() / array_filter()

array_map() / array_filter() Since: PHP 4(2000)

Functions that apply a callback to each element of an array to transform it, or extract elements based on a condition.

Syntax

// Applies a callback function to each element of an array and returns a new array.
array_map(callback, array1, array2, ...);

// Evaluates each element with a callback function and returns only the elements that pass the condition.
array_filter(array, callback, flag);

Functions

FunctionDescription
array_map($callback, $array, ...)Applies a callback function to each element of an array and returns a new array composed of the return values. The original array is not modified.
array_filter($array, $callback, $flag)Returns an array containing only the elements for which the callback function returns true. If the callback is omitted, empty values are removed.

Sample Code

<?php
// Creates a new array with each element doubled.
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map(fn($n) => $n * 2, $numbers);
print_r($doubled); // Results in an array of [2, 4, 6, 8, 10].

// Converts all strings in an array to uppercase.
$words = ['hello', 'world', 'php'];
$upper = array_map('strtoupper', $words);
print_r($upper); // Results in ['HELLO', 'WORLD', 'PHP'].

// Processes the values of an associative array.
$prices = ['apple' => 150, 'orange' => 80, 'grape' => 300];
$with_tax = array_map(fn($price) => (int)($price * 1.1), $prices);
print_r($with_tax); // Each price has 10% tax added.

// Extracts only even numbers.
$nums = [1, 2, 3, 4, 5, 6, 7, 8];
$evens = array_filter($nums, fn($n) => $n % 2 === 0);
print_r($evens); // Only the even numbers [2, 4, 6, 8] remain.

// Removes empty values. Omitting the callback removes them automatically.
$data = ['Taro', '', 'Hanako', null, 'Jiro', 0, false];
$clean = array_filter($data);
print_r($clean); // All empty values are removed.

// Extracts elements from an associative array that meet a condition.
$stock = ['apple' => 5, 'orange' => 0, 'grape' => 12, 'peach' => 0];
$available = array_filter($stock, fn($qty) => $qty > 0);
print_r($available); // Only items with stock remaining are kept.

Notes

array_map() transforms each element of an array and returns a new array. The callback can be specified as a function name string, an anonymous function, or an arrow function. When multiple arrays are passed, elements at the same index from each array are passed as separate arguments to the callback.

array_filter() extracts only the elements that meet a condition. If the callback is omitted, it removes all falsy values such as false, null, empty strings, and 0. Since 0 is also removed, explicitly specify a condition in the callback if you need to keep numeric zeros.

The result of array_filter() preserves the original keys, which may leave gaps in the index. If you need sequential keys, use array_values() to re-index the array. For aggregating arrays, see array_reduce().

If you find any errors or copyright issues, please .