How to Get the First Element of an Array in PHP: 7 Working Ways

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

Arrays in PHP: The Eternal Puzzle of the First Element

\n\n

Every PHP developer has encountered the task at least once: "How do I get the first element of an array?" It seems simple enough, right? But as we know, the devil is in the details. Arrays in PHP can be indexed (with numeric keys) or associative (with string keys), and what works for one type may fail for another.

\n\n

In this article, we'll cover 7 proven ways to get the first element of an array, compare their performance, and learn which method to choose in each specific situation. You'll discover why array_shift() isn't always the best choice, and how reset() can save your code.

\n\n

Table of Contents

\n\n\n

Method 1: array_shift() — The Classic

\n

The most obvious method that comes to mind is using the array_shift() function. It extracts the first element of the array and removes it from the original array.

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry'];\n$first = array_shift($fruits);\necho $first; // apple\nprint_r($fruits); // [1 => 'banana', 2 => 'cherry']\n?>
\n\n

Pros: Simplicity, intuitiveness.

\n

Cons: The function modifies the original array. If you need to preserve the data, this method is not suitable. Additionally, for large arrays, the operation can be costly.

\n\n

Method 2: reset() — The Clever Pointer Trick

\n

The reset() function sets the internal array pointer to the first element and returns its value. The array itself is not modified!

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry'];\n$first = reset($fruits);\necho $first; // apple\nprint_r($fruits); // ['apple', 'banana', 'cherry'] — array untouched\n?>
\n\n

This method works perfectly with both indexed and associative arrays. reset() is my personal favorite for most tasks.

\n\n

Important: If the array is empty, reset() will return false. Keep this in mind!

\n\n

Method 3: current() after reset() — For Control

\n

Sometimes you need not only to get the first element but also to ensure the pointer is positioned on it. The combination of reset() + current() gives you full control.

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry'];\nreset($fruits);\n$first = current($fruits);\necho $first; // apple\n// Pointer remains on the first element\n$second = next($fruits);\necho $second; // banana\n?>
\n\n

This method is useful when you plan to iterate through the array using next(), prev(), and other pointer manipulation functions.

\n\n

Method 4: array_values() + [0] — The Universal Soldier

\n

If you're working with an associative array and don't know the key of the first element, you can reset all keys using array_values() and access the zero index.

\n\n
<?php\n$user = ['name' => 'John', 'age'                

Blogs

Book Recommendations