How to get the first item from an associative PHP array?

How to get the first item from an associative PHP array?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get the first item from an associative PHP array?

Retrieving the first value from an associative array is a common task, but it often trips up developers because of how PHP structures these data types internally. When you define an array like $array['foo'] = 400; followed by $array['bar'] = 'xyz';, you might instinctively think the "first" item corresponds to the first key defined in your code, or perhaps the one with the lowest alphabetical value.

In PHP's internal representation, associative arrays are stored as hash tables. While iterating through them with foreach yields keys and values based on insertion order (since PHP 5.4), you cannot simply access an element using a numeric index like $array[0]. Attempting to do so will result in a notice or return NULL, because the array does not contain a numeric key at that position. The keys are strings, even if they happen to be numeric strings like '1' or '2'.

To reliably retrieve the first item without knowing its specific key, you need to iterate through the array until you find the first entry. The most direct approach utilizes array_shift, but there is a critical distinction in how it behaves with associative arrays versus indexed arrays.

If you use array_shift on an associative array, PHP removes and returns the first element based on its internal insertion order. However, this comes with a significant side effect: it modifies the original array by re-indexing it or removing the entry entirely. This is often not what you want when simply reading data. If your code relies on the integrity of the original $array structure, using array_shift will break subsequent logic that expects those specific keys to remain accessible.

A safer alternative is to use a loop with current() and next(), or iterate manually. The standard pattern involves checking if the array is empty first to avoid errors, then moving the internal pointer to the next element before accessing it.

if (!empty($array)) {
    $first = current($array);
}

In this example, current($array) returns the value of the element corresponding to the current array pointer. By default, when an array is created or accessed, the pointer starts at the first element. This method allows you to read the value without altering the original array structure. If you needed to move past that element later, you could chain next($array) to advance the internal cursor.

Another robust option available in modern PHP versions (7.2+) is using array_values() combined with indexing, though this also creates a new indexed array copy, which incurs memory overhead. For large datasets, this duplication might be undesirable. If performance is critical and you only need the first value, iterating once or using current() is computationally cheaper than creating a full copy of the dataset.

The trade-off here lies between readability and side effects. Functions like reset() move the internal pointer back to the beginning, which is useful if you plan to iterate multiple times but want to ensure you start from the first item every time. However, relying on internal pointers can make code harder to debug if other parts of your application manipulate those pointers unexpectedly.

For Laravel developers, working with Eloquent collections or facades often abstracts these low-level array behaviors, but when dealing with raw arrays passed into utility functions, understanding these nuances prevents subtle bugs where data structures are silently modified or keys become inaccessible. The best practice is to choose the method that preserves your data integrity unless you explicitly intend to manipulate the order or remove elements.

Our network