in this php article, We’ll learn how to get the first element of an array using php in different ways with some examples. There are three types of arrays in PHP that can be used to fetch the elements from the array:
The PHP provides several methods to get the first element of an array. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more.
Also checkout other related tutorials,
Merge Two Array or Multiple Array in PHP
How To Convert XML To Associative Array in PHP
Remove Duplicates From Multidimensional Array
How To Convert XSD into Array Using PHP
There are the following types of arrays available in PHP.
It is an array with a numeric key.
array('apple', 'orange', 'Guava')
It is used to store key-value pairs.
array("a" => 'apple', "b" => 'orange', "c" => 'Guava')
It is a type of array that stores another array at each index instead of a single element.
array("a" => 'apple', "b" => 'orange', "c" => 'Guava')
Let’s discuss the different ways to access the first element of an array using PHP. We have followed a php array, which has a list of fruit names. I would like to get the first element of this array.
array('apple', 'orange', 'Guava')
The Expected result would be: apple
We can access the first element of an array using index as like below:
Output: apple
The PHP foreach method works on both array and objects. The foreach loop iterates over an array of elements.
<?php // element of the array $array = array( 1 => 'apple', 2 => 'orange', 3 => 'Guava' ); foreach($array as $name) { echo $name; // Break loop after first iteration break; } ?>
Output: apple
The reset() function rewinds array’s internal pointer to the first element and returns the value of the first array element, or FALSE if the array is empty.
<?php // element of the array $array = array( 1 => 'apple', 2 => 'orange', 3 => 'Guava' ); //get first element echo reset($array); ?>
Output: apple
The current() method is also used to get first array element. Every array has an internal pointer to its “current” element, which is initialized to the first element inserted into the array.
<?php // PHP program to access the first $array = array('apple', 'orange', 'Guava') echo current($array); ?>
Output: apple
So it works until you have re-positioned the array pointer.
Sometimes. we need to get the first element key, then we can get the key by following code: (execute it after reset).
<?php // element of the array $array = array( 1 => 'apple', 2 => 'orange', 3 => 'Guava' ); $first_value = reset($array); echo $first_key = key($array); ?>
Output: 1
PHP 7.3 added two functions for getting the first and the last key of an array directly without modification of the original array and without creating any temporary objects:
<?php // element of the array $array = array( 1 => 'apple', 2 => 'orange', 3 => 'Guava' ); $first_key = array_key_first($array); $last_key = array_key_last($array); $first_value = $array[$first_key]; $last_value = $array[$last_key]; echo "first_key :".$first_key."<br>"; echo "last_key :".$last_key."<br>"; echo "first_value :".$first_value."<br>"; echo "last_value :".$last_value."<br>"; ?>
Output:
1 3 apple Guava
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More