0Pricing
PHP Academy · Lesson

Array Transformation Functions

Apply array_map, array_filter, and array_reduce.

Functional Array Transforms

PHP provides three powerful higher-order functions for transforming arrays without explicit loops:

  • array_map() — transform each element
  • array_filter() — keep elements that pass a test
  • array_reduce() — reduce to a single value

array_map()

Apply a callback to every element and return a new array:

<?php
$prices = [10, 20, 30, 40];

$doubled = array_map(fn($p) => $p * 2, $prices);
print_r($doubled); // [20, 40, 60, 80]

// Multiple arrays
$a = [1, 2, 3];
$b = [10, 20, 30];
$sums = array_map(fn($x, $y) => $x + $y, $a, $b);
print_r($sums); // [11, 22, 33]

All lessons in this course

  1. Indexed and Associative Arrays
  2. Multidimensional Arrays
  3. Sorting and Searching Arrays
  4. Array Transformation Functions
← Back to PHP Academy