Get First Element Of Array In PHP

In this tutorial we will see How To Get First Element Of Array In PHP. PHP foreach loop, array_key_first function, array_values() function, reset() function and current() function can be used to get the first element of array.

Using PHP foreach loop

PHP foreach loop loops through each key/value pair of an array. It only works with array.

We can use foreach loop in PHP to get the first element of array. This is simplest and probably the best method to get it done.

Take a look at the code given below.

<?php
$array = ['HTML', 'CSS', 'JavaScript', 'PHP', 'JQUERY'];
foreach($array as $value)
{
$first_element = $value;
break;
}
echo $first_element;
?>

Output

HTML

In above example we have used foreach loop to get the first element of array.

We have used break statement to exit the loop on first iteration. This will always get us the first element of array.

Using PHP array_values() Function

PHP array_values() Function returns an array of all values of the array but this array will have numeric keys starting from 0. Which means for the first value or first element of array we can use 0 as a key.

That is why we can use array_values() to get the first element of array, see the example below.

<?php
$array = ['HTML', 'CSS', 'JavaScript', 'PHP', 'JQUERY'];
$first_element = array_values($array)[0];
echo $first_element;
?>

Output

HTML

Using PHP array_key_first() Function

PHP array_key_first() Function gives the first key of array.

Using this first key, you can get the first element of array. This method will only work with PHP 7.3 or higher.

Take a look at the code given below.

<?php
$array = ['HTML', 'CSS', 'JavaScript', 'PHP', 'JQUERY'];
$key = array_key_first($array);
$first_element = $array[$key];
echo $first_element;
?>

Output

HTML

Using PHP reset() Function

PHP reset() Function moves the internal pointer to the first element of the array.

reset() Function can also be used to get the first element of array. It returns first element of array if it's not empty, otherwise False is returned.

<?php
$array = ['HTML', 'CSS', 'JavaScript', 'PHP', 'JQUERY'];
$first_element = reset($array);
echo $first_element;
?>

Output

HTML

Using PHP current() Function

PHP current() Function returns the element of array which is currently being pointed by the internal pointer of array.

current() Function is often used to get the first element of array in PHP.

<?php
$array = ['HTML', 'CSS', 'JavaScript', 'PHP', 'JQUERY'];
$first_element = current($array);
echo $first_element;
?>

Output

HTML

How To Append a String in PHP How To Prepend a String in PHP Replace a Word In String in PHP Convert Special Characters To HTML Entities PHP Find Number of Words in a String in PHP How To Find String Length in PHP How To Convert String To Uppercase in PHP How To Convert String To Lowercase in PHP How to Combine Two Strings in PHP Delete Array Elements By Value in PHP Get First Element Of Array In PHP Remove Numbers From String in PHP Convert Timestamp To Date in PHP Convert a Date to Timestamp in PHP Get Current Page Slug in PHP Check If Session Is Started in PHP Split Sentence Into Words in PHP Remove Last Character From String in PHP PHP Remove Spaces From String Replace Space With Dash in PHP Remove Everything from String Except Numbers Using PHP Get Full Url Address Using PHP Automatic Copyright Year Using PHP Generate 4 Digit Random Number in PHP How To Check If a Number is Prime using PHP Convert Integer to String in PHP