Iterating over arrays is one of the most common tasks in PHP programming. Whether you’re working with indexed, associative, or multidimensional arrays, you need to know how to conveniently loop through elements, modify them, and process data. In this lesson, you’ll learn three key tools:
- the foreach loop,
- the arraywalk function (and briefly arraywalkrecursive),
- the arraychunk function.
Basics: when to use foreach, arraywalk, and arraychunk
foreach: the simplest way to iterate
- foreach is designed for conveniently looping through array elements.
- You can easily access the value and key, and even modify elements using references.
- Ideal for most tasks: displaying, simple transformations, summation.
arraywalk: iteration with a callback function
- arraywalk calls a given function for each array element.
- Thanks to the callback, you can encapsulate logic and reuse it easily.
- Can modify elements if you work with references.
arraychunk: splitting an array into smaller pieces
- arraychunk doesn’t iterate directly but divides an array into chunks of specified size.
- Perfect for pagination, batch processing, or limiting memory usage.
PHP Code Examples (Step by Step)
foreach: basics
Iterating an indexed array
<?php
$numbers = [10, 20, 30];
foreach ($numbers as $value) {
echo "Number: $value\n";
}
Iterating an associative array (key => value)
<?php
$user = [
'name' => 'Alice',
'age' => 25,
'city' => 'Krakow',
];
foreach ($user as $key => $value) {
echo "Key: $key, Value: $value\n";
}
Modifying elements by reference
<?php
$products = ['bread', 'milk', 'eggs'];
foreach ($products as &$p) {
$p = strtoupper($p);
}
unset($p); // Always unset the reference after foreach
print_r($products);
// ['BREAD', 'MILK', 'EGGS']
Breaking and skipping iterations
<?php
$nums = [1, 2, 3, 4, 5];
foreach ($nums as $n) {
if ($n === 3) {
continue; // skip 3
}
if ($n > 4) {
break; // stop at 5
}
echo $n . PHP_EOL;
}
// Output: 1, 2, 4
Iterating a multidimensional array
<?php
$orders = [
['id' => 1, 'amount' => 99.99],
['id' => 2, 'amount' => 149.50],
];
foreach ($orders as $order) {
echo "Order #{$order['id']} - amount: {$order['amount']}\n";
}
Destructuring in foreach (PHP 7.1+)
<?php
$points = [
[2, 3],
[5, 7],
[10, 1],
];
foreach ($points as [$x, $y]) {
echo "Point X=$x, Y=$y\n";
}
arraywalk: iteration with callback
Basic example with anonymous function
<?php
$names = ['alice', 'olivia', 'zenek'];
array_walk($names, function (&$value, $key) {
$value = ucfirst($value);
});
print_r($names); // ['Alice', 'Olivia', 'Zenek']
Using named function or static method as callback
<?php
function trimAndLowercase(&$v, $k): void {
$v = strtolower(trim($v));
}
$data = [' ALICE ', ' Olivia', "ZENEK "];
array_walk($data, 'trimAndLowercase');
print_r($data); // ['alice', 'olivia', 'zenek']
class Normalizer {
public static function normalize(&$v, $k): void {
$v = preg_replace('/\s+/', ' ', trim($v));
}
}
$texts = [" lorem ipsum ", "dolor sit"];
array_walk($texts, [Normalizer::class, 'normalize']);
print_r($texts); // ["lorem ipsum", "dolor sit"]
arraywalk with user parameter
<?php
$amounts = [100, 200, 300];
array_walk($amounts, function (&$v, $k, $percent) {
$v = $v * (1 + $percent);
}, 0.23);
print_r($amounts); // [123, 246, 369]
arraywalkrecursive (nested arrays)
<?php
$data = [
'user' => ['name' => ' Alice ', 'email' => ' MAIL@EXAMPLE.COM '],
'tags' => [' PHP ', ' Arrays '],
];
array_walk_recursive($data, function (&$v, $k) {
if (is_string($v)) {
$v = strtolower(trim($v));
}
});
print_r($data);
// 'name' => 'alice', 'email' => 'mail@example.com', 'tags' => ['php','arrays']
⚠️ Note: unlike arraymap, arraywalk does not return a new array — it modifies the original in place.
arraychunk: splitting arrays into parts
Basics
<?php
$nums = [1,2,3,4,5,6,7];
$chunks = array_chunk($nums, 3);
print_r($chunks);
// [[1,2,3],[4,5,6],[7]]
Preserving keys
<?php
$map = [10 => 'a', 20 => 'b', 30 => 'c', 40 => 'd'];
$chunks = array_chunk($map, 2, true);
print_r($chunks);
// [[10=>'a',20=>'b'], [30=>'c',40=>'d']]
Pagination example
<?php
$results = range(1, 50);
$perPage = 10;
$pages = array_chunk($results, $perPage);
$pageNum = 2;
$currentPage = $pages[$pageNum - 1] ?? [];
foreach ($currentPage as $record) {
echo "Record: $record\n";
}
Batch processing
<?php
$rows = range(1, 1000);
foreach (array_chunk($rows, 100) as $batch) {
foreach ($batch as $r) {
// process
}
}
Best Practices and Common Mistakes
Best Practices
- Prefer foreach for standard iteration.
- When modifying elements, use foreach with reference or arraywalk with reference. Always unset references after foreach.
- Use arraywalk for applying a single consistent operation across all elements.
- Use arraychunk for pagination or batch processing.
- Keep callbacks small and meaningful.
- Use descriptive variable names and comments.
- Ensure correct data types with type hints or strict types.
Common Mistakes
- Forgetting
unset($v)
afterforeach ($arr as &$v)
— can cause unexpected behavior. - Expecting arraywalk to return a new array — it doesn’t.
- Modifying array structure during foreach — may cause unexpected results.
- Using arraywalk when arraymap is more appropriate.
- Forgetting to preserve keys in arraychunk when needed.
- Overusing callbacks for trivial logic — foreach is clearer.
- Nested loops on large arrays without optimization.
Summary
- foreach: simplest, most common iteration structure; supports references.
- arraywalk: applies a callback to each element, modifies in place.
- arraychunk: splits arrays into chunks; useful for pagination/batch processing.
- Follow best practices: clean up references, use correct tools, comment code, avoid modifying structure mid-loop.
Mini Quiz
- Output?
$items = ['a','b','c'];
foreach ($items as &$v) {
$v = strtoupper($v);
}
unset($v);
echo implode(',', $items);
➡️ A,B,C
- Which is true about arraywalk?
- B: Modifies in place via reference.
- C: Works only on indexed arrays.
- What does third param
true
in arraychunk(\$arr,2,true) do?
- Which to use for batch processing 100 elements?
- Common mistake with foreach by reference?
Now you have solid foundations for iterating over arrays in PHP. Next, we’ll combine these with filtering and mapping data to show practical PHP programming patterns.