Arrays in PHP: The Eternal Puzzle of the First Element
\n\nEvery 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\nIn 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.
Table of Contents
\n- \n
- Method 1: array_shift() — The Classic \n
- Method 2: reset() — The Clever Pointer Trick \n
- Method 3: current() after reset() — For Control \n
- Method 4: array_values() + [0] — The Universal Soldier \n
- Method 5: array_key_first() — For PHP 7.3+ \n
- Method 6: foreach with break — A Bicycle, But It Works \n
- Method 7: array_slice() — For Functional Programming Fans \n
- Performance Comparison \n
- Common Beginner Mistakes \n
- Conclusions and Action Items \n
Method 1: array_shift() — The Classic
\nThe 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.
<?php\n$fruits = ['apple', 'banana', 'cherry'];\n$first = array_shift($fruits);\necho $first; // apple\nprint_r($fruits); // [1 => 'banana', 2 => 'cherry']\n?>\n\nPros: Simplicity, intuitiveness.
\nCons: 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\nMethod 2: reset() — The Clever Pointer Trick
\nThe reset() function sets the internal array pointer to the first element and returns its value. The array itself is not modified!
<?php\n$fruits = ['apple', 'banana', 'cherry'];\n$first = reset($fruits);\necho $first; // apple\nprint_r($fruits); // ['apple', 'banana', 'cherry'] — array untouched\n?>\n\nThis method works perfectly with both indexed and associative arrays. reset() is my personal favorite for most tasks.
Important: If the array is empty, reset() will return false. Keep this in mind!
Method 3: current() after reset() — For Control
\nSometimes 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.
<?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\nThis method is useful when you plan to iterate through the array using next(), prev(), and other pointer manipulation functions.
Method 4: array_values() + [0] — The Universal Soldier
\nIf 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.
<?php\n$user = ['name' => 'John', 'age'