Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • Last Updated : 01 Aug, 2021

Problem Statement: How to access the first element of an array in PHP?

Solution: There are mainly three types of arrays in PHP:

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

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.

  • By direct accessing the 0th index:




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




    <?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 used to move the array’s internal pointer to the first element.




    <?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 ]] )




    <?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 all the values of an array.
    Syntax:

    array array_values ( array $array )




    <?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 array.
    Syntax:

    mixed array_pop ( array &$array )

    By default array_reverse() 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.




    <?php
    // PHP program to access the first 
    // element of the array
    $array = array(
        33 => 'geeks'
        36 => 'for'
        42 => 'computer'
    );
    echo array_pop(array_reverse($array));
    ?>
    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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!