Php

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:

Related Post

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

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories