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

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    geeks
    
  • Using foreach loop:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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
    }
    ?>

    chevron_right

    
    

    Output:

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    geeks
    
  • Using array_values() function: This function return all the values of an array.
    Syntax:

    array array_values ( array $array )

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    geeks
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.