Как получить первый элемент массива в PHP?

Опубликовано: 4 Февраля, 2022

Постановка проблемы: как получить доступ к первому элементу массива в PHP?

Решение: в PHP есть в основном три типа массивов:

  1. Индексированный массив
  2. Ассоциативный массив
  3. Многомерный массив

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