foreach index in PHP: 3 best ways in 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The eternal question of foreach and its index

\n\n

If you've ever written in PHP, you've probably encountered a situation: you need not just to iterate over array elements, but also to know the sequence number of the current iteration. It seems simple, right? But this very question — "How to find the foreach index?" — gathers thousands of views on Stack Overflow. And for good reason!

\n\n

In 2025, as PHP continues to evolve and projects become more complex, understanding how to correctly work with indexes in a foreach loop is not just a whim, but a necessity. The readability of your code, its performance, and how easily your colleague (or yourself in six months) can maintain it all depend on this.

\n\n

In this article, we'll break down three proven ways to get the index of an element in foreach, show real code examples, discuss pitfalls, and give recommendations you can apply today. Let's go!

\n\n

Method #1: Using array keys (the most elegant)

\n\n

The simplest and most "native" way for PHP is to use the foreach ($array as $key => $value) syntax. It doesn't require additional variables or functions and works with any arrays, whether they are lists with numeric keys or associative arrays.

\n\n

How does it work?

\n

Imagine you have an array of users:

\n\n
<?php\n$users = ['Alice', 'Bob', 'Charlie', 'Diana'];\n\nforeach ($users as $index => $name) {\n    echo "User #{$index}: {$name}<br>";\n}\n?>
\n\n

The result will be:

\n
User #0: Alice\nUser #1: Bob\nUser #2: Charlie\nUser #3: Diana
\n\n

Note: $index is not just a counter. It is the actual key of the array. If the array has non-standard keys (e.g., strings), then $index will contain those keys.

\n\n

When to use this?

\n
    \n
  • Whenever possible. This is the most readable and performant way.
  • \n
  • When you don't need to reset the counter or manage it manually.
  • \n
  • For any type of array: both lists and associative arrays.
  • \n
\n\n

The pitfall

\n

If the array has "gaps" in the indexes (for example, after deleting an element), then $index will equal the key, not the sequence number. Example:

\n\n
<?php\n$fruits = [0 => 'apple', 2 => 'banana', 5 => 'cherry'];\n\nforeach ($fruits as $key => $fruit) {\n    echo "Key: {$key}, fruit: {$fruit}<br>";\n}\n?>
\n\n

Result:

\n
Key: 0, fruit: apple\nKey: 2, fruit: banana\nKey: 5, fruit: cherry
\n\n

If you need a sequential sequence number (0, 1, 2), this method won't work — you need to use the second option.

\n\n

Method #2: Manual counter (flexibility and control)

\n\n

The classic approach is to create a counter variable and increment it on each iteration. This gives you full control over the numbering.

\n\n

Basic example

\n\n
<?php\n$items = ['book', 'pen', 'notebook', 'pencil'];\n$counter = 0;\n\nforeach ($items as $item) {\n    echo "Item #{$counter}: {$item}<br>";\n    $counter++;\n}\n?>
\n\n

Result:

\n
Item #0: book\nItem #1: pen\nItem #2: notebook\nItem #3: pencil
\n\n

Counter starting from 1

\n

Often you need to display numbering starting from 1 to the user. This is easy to do:

\n\n
<?php\n$tasks = ['Wash dishes', 'Walk the dog', 'Do homework'];\n$taskNumber = 1;\n\nforeach ($tasks as $task) {\n    echo "Task #{$taskNumber}: {$task}<br>";\n    $taskNumber++;\n}\n?>
\n\n

Conditional counter control

\n

A manual counter allows you to skip elements or reset the numbering:

\n\n
<?php\n$numbers = [1, 2, 0, 3, 4, 0, 5];\n$v                

Blogs

Book Recommendations