How to get the First Element of an Array in PHP?
Last Updated :
11 Jul, 2025
Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.
Below are the approaches to get the first element of an array in PHP:
Approach 1: Direct Index Access
In this approach, the first element of the array is accessed directly by using the index 0. PHP arrays are zero-based, so $array[0] retrieves the first element.
Example: In this example we accesses the first element of an indexed array by directly using the index 0. It outputs 'geeks', the value stored at the first position in the $array.
PHP
<?php
// PHP program to access the first
// element of the array
$array = array('geeks', 'for', 'computer');
echo $array[0];
?>
Approach 2: Using foreach Loop
In this approach, a foreach loop iterates through the array. The loop retrieves each element, and by using a break statement after the first iteration, it effectively accesses only the first element.
Syntax:
foreach( $array as $key => $element) {
// PHP Code to be executed
}
Example: In this example we accesses the first element of an associative array using a foreach loop. It outputs 'geeks' and uses break to terminate the loop after the first iteration.
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
Approach 3: Using reset() Function
The reset() function moves the internal pointer of an array to its first element and returns that value. It's useful for both indexed and associative arrays, providing an efficient way to access the first element directly.
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);
?>
Approach 4: Using array_slice() Function
The array_slice() function extracts a portion of an array based on a specified offset and length. To access the first element, set the offset to 0 and length to 1, effectively returning the first element in a sub-array.
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];
?>
Approach 5: Using array_values() Function
The array_values() function returns all the values of an array as a new indexed array, re-indexing any keys numerically from zero. By using $array_values[0], you can easily access the first element of the original array.
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];
?>
Approach 6: Using array_pop() Function
The array_pop() function removes and returns the last element of an array. By combining it with array_reverse(), you can reverse the array and then use array_pop() to access and retrieve the original array's first element.
Syntax:
mixed array_pop ( array &$array )
Example: In this example we reverses an associative array using array_reverse() and then uses array_pop() to remove and return the last element of the reversed array, effectively retrieving the first element, 'geeks'.
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);
?>
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.
Similar Reads
Interview Preparation
Practice @Geeksforgeeks
Data Structures
Algorithms
Programming Languages
Web Technologies
Computer Science Subjects
Data Science & ML
Tutorial Library
GATE CS