0Pricing
PHP Academy · Lesson

Array Transformation Functions

Apply array_map, array_filter, and array_reduce.

Array Transformation Functions is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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]

array_filter()

Keep elements that satisfy a callback. Without callback, removes falsy values:

<?php
$numbers = [1, 2, 3, 4, 5, 6];

$evens = array_filter($numbers, fn($n) => $n % 2 === 0);
print_r($evens); // [1=>2, 3=>4, 5=>6] — keys preserved!

$reindexed = array_values($evens); // re-index

$mixed = [0, 'hello', '', null, false, 42];
print_r(array_filter($mixed)); // [1=>'hello', 5=>42]

array_reduce()

Reduce an array to a single value by applying a callback cumulatively:

<?php
$cart = [
    ['name' => 'Widget', 'price' => 10],
    ['name' => 'Gadget', 'price' => 25],
    ['name' => 'Doohickey', 'price' => 5],
];

$total = array_reduce($cart, fn($carry, $item) => $carry + $item['price'], 0);
echo $total;  // 40

Chaining Transformations

Chain map, filter, and reduce for expressive data pipelines:

<?php
$scores = [45, 80, 30, 90, 55, 70];

$passingAvg = array_sum(
    array_filter($scores, fn($s) => $s >= 50)
) / count(array_filter($scores, fn($s) => $s >= 50));

echo $passingAvg;  // 74.75

array_map with Keys

array_map doesn't pass keys. Use array_walk when you need both key and value:

<?php
$prices = ['apple' => 1.0, 'banana' => 0.5, 'cherry' => 2.0];

$formatted = [];
array_walk($prices, function($price, $name) use (&$formatted) {
    $formatted[$name] = '$' . number_format($price, 2);
});

print_r($formatted);

array_chunk()

Split an array into chunks of a given size:

<?php
$items = range(1, 10);
$batches = array_chunk($items, 3);
print_r($batches);
// [[1,2,3], [4,5,6], [7,8,9], [10]]

array_combine()

Create an associative array by combining a keys array with a values array:

<?php
$keys   = ['name', 'age', 'city'];
$values = ['Alice', 30, 'London'];

$person = array_combine($keys, $values);
print_r($person);
// ['name'=>'Alice','age'=>30,'city'=>'London']

array_zip with array_map

PHP has no native zip, but array_map(null, ...) creates a zip-like structure:

<?php
$names  = ['Alice', 'Bob'];
$scores = [90, 85];

$zipped = array_map(null, $names, $scores);
print_r($zipped);
// [['Alice', 90], ['Bob', 85]]

array_map Preserves Keys

On a single array, array_map preserves keys. Use array_values to re-index:

<?php
$assoc = ['a' => 1, 'b' => 2, 'c' => 3];

$result = array_map(fn($v) => $v * 10, $assoc);
print_r($result);
// ['a'=>10, 'b'=>20, 'c'=>30] — keys preserved

array_sum and array_product

Quick reduction helpers for numeric arrays:

<?php
$nums = [1, 2, 3, 4, 5];

echo array_sum($nums);      // 15
echo array_product($nums);  // 120 (1*2*3*4*5)

// Works with floats too
$prices = [1.5, 2.5, 3.0];
echo array_sum($prices);    // 7.0

Quick Check

Which function applies a callback to every element and returns a new array?

Recap: Array Transformation Functions

Summary:

  • array_map() — transform each element into a new array
  • array_filter() — keep elements matching a predicate
  • array_reduce() — fold to a single accumulated value
  • array_chunk() — split into sub-arrays
  • array_combine() — build associative array from keys + values
  • array_sum/product() — quick numeric reductions

Frequently asked questions

Is the “Array Transformation Functions” lesson free?

Yes — the full text of “Array Transformation Functions” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Array Transformation Functions”?

Apply array_map, array_filter, and array_reduce. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Array Transformation Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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