Sentry Answers>PHP>

How can I get the first element of an array in PHP?

How can I get the first element of an array in PHP?

Nadia S.

The Problem

How can I get the first element of an array in PHP?

The Solution

We’ll show you three ways you can get the first element of an array in PHP:

  • Using the reset() and current() functions, which resets the array’s internal pointer without changing the array.
  • Reversing the array and using the array_pop() function, which preserves the original array and the position of it’s internal pointer.
  • Using the array_shift() function to remove the first element from the array.

Reset the Array’s Internal Pointer

An array’s internal pointer keeps track of which element in the array is currently being referenced. You can use the reset() function to move the internal pointer to the first element of the array, and then the current() function to get the value of the element that is being pointed to:

Click to Copy
$myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // ensure the pointer is at the first element reset($myArray); // get the value of the element being pointed to $firstElement = current($myArray); echo $firstElement;

We get the value of the first element:

Click to Copy
apple

Preserve the Position of the Array’s Internal Pointer

If you’re working with an array that you’re doing further manipulation on, you might need to preserve the position of the array’s internal pointer. You can preserve the position of the array’s internal pointer and get the first element by reversing the array and using the array_pop() function to remove and return the last element of the reversed array.

Click to Copy
$myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // create a reversed array of the original $revArray = array_reverse($myArray); // remove the last element $myElement = array_pop($revArray); // output the element and original array echo $myElement .PHP_EOL; print_r($myArray);

Here, we remove the last element of our reversed array, which gives us the first element of our original $myArray. The output gives us the value of the first element of our original array and the original array:

Click to Copy
apple Array ( [red] => apple [yellow] => banana [green] => grapes )

The array_shift() Function

In this example, we pass $myArray into the array_shift() function to get its first element:

Click to Copy
$myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // get the first element $firstFruit = array_shift($myArray); // output echo $firstFruit .PHP_EOL; print_r($myArray);

Our array_shift() function returns the value of the first element. We can see from the output that the original array no longer has that element:

Click to Copy
// the value of the first element apple // the original array has two remaining elements left Array ( [yellow] => banana [green] => grapes )
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.