# PHP Get First Array Element with reset and array_shift

> Retrieve the first element of a PHP array using reset() with current(), array_reverse() with array_pop(), or array_shift() for indexed and associative arrays

**URL:** https://sentry.io/answers/how-can-i-get-the-first-element-of-an-array-in-php/

---

## 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:

```php
$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:

```
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.

```php
$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:

```
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:

```php
$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:

```
// the value of the first element
apple

// the original array has two remaining elements left
Array
(
    [yellow] => banana
    [green] => grapes
)
```

---

*Source: [sentry.io/answers/how-can-i-get-the-first-element-of-an-array-in-php/](https://sentry.io/answers/how-can-i-get-the-first-element-of-an-array-in-php/)*
