Open In App

How to get the first element of an array in PHP?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article, we will see how to get access to the first element of an array in PHP, & also understand their implementation through the examples. There are mainly 3 types of arrays in PHP that can be used to fetch the elements from the array:

  • Indexed Array: It is an array with a numeric key. It is basically an array wherein each of the keys is associated with its own specific value.
  • Associative Array: It is used to store key-value pairs.
  • Multidimensional Array: It is a type of array which stores another array at each index instead of a single element. In other words, define multi-dimensional arrays as array of arrays. 

There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more. We will discuss the different ways to access the first element of an array sequentially.

By direct accessing the 0th index:

PHP




<?php
    
  // PHP program to access the first 
  // element of the array
  $array = array('geeks', 'for', 'computer');
  echo $array[0];
?>


Output:

geeks

Using foreach loop: The foreach construct works on both array and objects. The foreach loop iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.

Syntax:

foreach( $array as $key => $element) {
    // PHP Code to be executed
}

Example: This example illustrates the foreach loop in PHP.

PHP




<?php 
  
  // PHP program to access the first 
  // element of the array
  $array = array(
      33 => 'geeks'
      36 => 'for'
      42 => 'computer'
  );
  
  foreach($array as $name) {
      echo $name;
  
      // Break loop after first iteration
      break
  }
?>


Output:

geeks

Using reset() function: The reset() function is used to move the array’s internal pointer to the first element.

Syntax:

reset($array)

Example: This example illustrates the use of the reset() function that helps to move any array’s internal pointer to the first element of that array.

PHP




<?php 
  // PHP program to access the first 
  // element of the array
  $array = array(
      33 => 'geeks'
      36 => 'for'
      42 => 'computer'
  );
  echo reset($array);
?>


Output:

geeks

Using array_slice() function: array_slice() returns the sequence of elements from the array as specified by the offset and length parameters.

Syntax:

array array_slice ( array $array, int $offset [, int $length = NULL [, 
                                        bool $preserve_keys = FALSE ]] )

Example: This example illustrates the array_slice() Function to fetch a part of an array by slicing through it, according to the user’s choice.

PHP




<?php
    
  // PHP program to access the first 
  // element of the array
  $array = array(
      33 => 'geeks'
      36 => 'for'
      42 => 'computer'
  );
  echo array_slice($array, 0, 1)[0];
?>


Output:

geeks

Using array_values() function: This function return an array of values from another array that may contain key-value pairs or just values.

Syntax:

array array_values ( array $array )

Example: This example describes the use of the array_values() function.

PHP




<?php
    
  // PHP program to access the first 
  // element of the array
  $array = array(
      33 => 'geeks'
      36 => 'for'
      42 => 'computer'
  );
  echo array_values($array)[0];
?>


Output:

geeks

Using array_pop() function: This function pop the element off the end of the array.

Syntax:

mixed array_pop ( array &$array )

By default, the array_reverse() function will reset all numerical array keys to start counting from zero while literal keys will remain unchanged unless a second parameter preserve_keys is specified as TRUE. This method is not recommended as it may do unwanted longer processing on larger arrays to reverse them prior to getting the first value.

Example: This example describes the use of the array_pop function.

PHP




<?php
    // PHP program to access the first
    // element of the array
    $array = array(
        33 => 'geeks',
        36 => 'for',
        42 => 'computer'
    );
    $arr = array_reverse($array);
    echo array_pop($arr);
?>


Output:

geeks

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Last Updated : 02 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads