Get the First Element of an Array Using PHP

in this php article, We’ll learn how to get the first element of an array using php in different ways with some examples. There are three types of arrays in PHP that can be used to fetch the elements from the array:

The PHP provides several methods to get the first element of an array. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more.

Also checkout other related tutorials,

Merge Two Array or Multiple Array in PHP
How To Convert XML To Associative Array in PHP
Remove Duplicates From Multidimensional Array
How To Convert XSD into Array Using PHP

There are the following types of arrays available in PHP.

Indexed Array

It is an array with a numeric key.

array('apple', 'orange', 'Guava')

Associative Array

It is used to store key-value pairs.

array("a" => 'apple', "b" => 'orange', "c" => 'Guava')

Multidimensional Array

It is a type of array that stores another array at each index instead of a single element.

array("a" => 'apple', "b" => 'orange', "c" => 'Guava')

How To Access the First Element of an Array in PHP

Let’s discuss the different ways to access the first element of an array using PHP. We have followed a php array, which has a list of fruit names. I would like to get the first element of this array.

array('apple', 'orange', 'Guava')

The Expected result would be: apple

Using Index

We can access the first element of an array using index as like below:

Output: apple

Using foreach loop:

The PHP foreach method works on both array and objects. The foreach loop iterates over an array of elements.

<?php 
  // element of the array
  $array = array(
      1 => 'apple', 
      2 => 'orange', 
      3 => 'Guava'
  );
  
  foreach($array as $name) {
      echo $name;
  
      // Break loop after first iteration
      break; 
  }
?>

Output: apple

Using reset() function

The reset() function rewinds array’s internal pointer to the first element and returns the value of the first array element, or FALSE if the array is empty.

<?php 
  // element of the array
  $array = array(
      1 => 'apple', 
      2 => 'orange', 
      3 => 'Guava'
  );
  //get first element
  echo reset($array);
?>

Output: apple

Using current()

The current() method is also used to get first array element. Every array has an internal pointer to its “current” element, which is initialized to the first element inserted into the array.

<?php    
  // PHP program to access the first
  $array = array('apple', 'orange', 'Guava')
  echo current($array);
?>

Output: apple

So it works until you have re-positioned the array pointer.

How to Get First Element Key in PHP

Sometimes. we need to get the first element key, then we can get the key by following code: (execute it after reset).

<?php 
  // element of the array
  $array = array(
      1 => 'apple', 
      2 => 'orange', 
      3 => 'Guava'
  );
  
  $first_value = reset($array);
  echo $first_key = key($array);
?>

Output: 1

Using array_key_first and array_key_last

PHP 7.3 added two functions for getting the first and the last key of an array directly without modification of the original array and without creating any temporary objects:

<?php 
  // element of the array
  $array = array(
      1 => 'apple', 
      2 => 'orange', 
      3 => 'Guava'
  );
  
  $first_key = array_key_first($array);
  $last_key = array_key_last($array);
  
  $first_value = $array[$first_key];
  $last_value = $array[$last_key];

  echo "first_key  :".$first_key."<br>";
  echo "last_key  :".$last_key."<br>";
  echo "first_value  :".$first_value."<br>";
  echo "last_value  :".$last_value."<br>";
?>

Output:

1
3
apple
Guava

Leave a Reply

Your email address will not be published. Required fields are marked *