How to Get the First Element of an Array in PHP: 5 Best 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

How to Get the First Element of an Array in PHP: Full Breakdown

\n\n

Have you ever wondered how many ways exist in PHP to get the first element of an array? It seems like a trivial task — yet there are five options, each with its own nuances. On Stack Overflow, this question has gathered thousands of views, and for good reason: beginners often fall into traps, while experienced developers debate performance.

\n\n

Today, we'll break down all the popular methods, their pros and cons, and find out which one is the fastest and safest. Let's go!

\n\n

1. Direct Index Access [0]

\n\n

The most obvious way is to access the zero index. But here lies the first pitfall. This method works only for arrays with numeric keys starting from zero.

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry'];\necho $fruits[0]; // apple\n\n// But what if keys don't start from zero?\n$users = [1 => 'Alice', 3 => 'Bob'];\necho $users[0]; // Notice: Undefined offset: 0\n?>
\n\n

Moreover, if the array is empty, you'll get an error. So, use this method only when you are 100% sure about the array structure.

\n\n

2. current() Function — The Unexpected Favorite

\n\n

Many are surprised, but current() returns not the current element of a loop, but the first element of the array (if the internal pointer hasn't been moved).

\n\n
<?php\n$colors = ['red', 'green', 'blue'];\necho current($colors); // red\n?>
\n\n

Important nuance: if you previously used next(), prev(), or end(), the pointer will be shifted. To guarantee getting the first element, call reset() beforehand.

\n\n
<?php\n$colors = ['red', 'green', 'blue'];\nnext($colors); // shift the pointer\necho current($colors); // green (no longer the first!)\n\nreset($colors); // reset the pointer\necho current($colors); // red\n?>
\n\n

3. reset() — Reset Pointer and Get the First Element

\n\n

The reset() function not only returns the first element but also resets the array's internal pointer to the beginning. This is especially useful before iteration.

\n\n
<?php\n$data = ['x' => 10, 'y' => 20, 'z' => 30];\n$first = reset($data);\necho $first; // 10\n\n// Now you can safely iterate\nforeach ($data as $key => $value) {\n    // We'll start from the first element\n}\n?>
\n\n

When to use: when you need to reliably get the first element, regardless of its key. Ideal for associative arrays.

\n\n

4. array_shift() — The Dangerous Handsome

\n\n

array_shift() extracts the first element and removes it from the array. This is a powerful tool, but with a side effect.

\n\n
<?php\n$queue = ['task1', 'task2', 'task3'];\n$firstTask = array_shift($queue);\n\necho $firstTask; // task1\nprint_r($queue);\n// Array ( [0] => task2 [1] => task3 )\n?>
\n\n

Note: keys are reindexed! If you had string keys, they will be preserved, but numeric ones will be reset. This can lead to unexpected bugs.

\n\n

When to use: in queue (FIFO) tasks, when an element needs to be not only read but also removed from the list.

\n\n

5. array_slice() — The Universal Soldier

\n\n

A safe method that returns a slice of the array without modifying the original. To get the first element, use array_slice($array, 0, 1).

\n\n
<?php\n$mixed = ['a' => 1, 'b' => 2, 'c' => 3];\n$firstPiece = array_slice($mixed, 0, 1);\n\nprint_r($firstPiece);\n// Array ( [a] => 1 )\n?>
\n\n

Important: this function returns an array, not a value. To get the value itself, add [0] or use current() on the result.

Blogs

Book Recommendations