PHP arrow functions with array_map and array_filter
PHP Dasun Kalupahana 80 views
<?php
$nums = range(1, 10);
$result = array_map(fn ($n) => $n ** 2, array_filter($nums, fn ($n) => $n % 2 === 0));
echo implode(' ', $result), PHP_EOL; // 4 16 36 64 100
$factor = 3;
echo implode(',', array_map(fn ($n) => $n * $factor, [1, 2, 3])), PHP_EOL; // 3,6,9
Arrow functions (fn) are one-line closures that capture outer variables by value automatically. Note that array_filter keeps keys, so wrap it in array_values() if you need a reindexed list. Needs PHP 7.4+.
Learn more on the blog
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
AI Coding Prompts That Actually Improve Productivity
Most lists of "best AI coding prompts" are collections of clever wording....
Laravel Herd Setup: Sites, PHP Versions, HTTPS and Databases
Laravel Herd is a local development environment for macOS and Windows that...