Home >Backend Development >PHP Tutorial >PHP Get First Element of Array

PHP Get First Element of Array

王林
王林Original
2024-08-29 12:43:361082browse

In PHP we have multiple methods to get the first value form the array. To get the first value we can directly pass the index, but there can be some situation where we do not know the index of the first element so for this we have several methods available in PHP which are in build. In PHP, to get the first element we have methods as, [pass_index], reset () method, array_values() method. All this method will return us the first element from the array. we will discuss all ways to get the first element from the array in PHP in detail from the next section.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

As we discussed we have several ways to get the first element from the array in PHP. Let’s discuss each of the syntaxes in detail we get an understating how to use this while programming see below;

1. By passing index;

$myarr = array("value1", "value2", "value3", "value4", "so on..");
$myarr[your_index];

2. reset() :

reset($your_array);

3. array_values ():

array_values($your_array)[your_index];

As you seen in the above syntax we have three different ways to get the first element from the array. Out of which two are methods which are already available in PHP and other one is direct passing of index.

How to Get First Element of Array in PHP?

There may be some situation where we need to access the first element from the array. But in order to access that we have the right to access and get its value from the array. For this, we have three ways defined in PHP to get the first element from the array. suppose we may have one requirement where we need to delete the first element from the array, so in that case, we can do this by using these methods and delete records. In this section, we will discuss all three ways in very much details to get an understanding of the methods and different ways to access the first element. Let’s start to see below;

1. By using the reset() Method

By the use of this method, we can easily access the first element from the array. This method always points the pointer to the first element from the array. Let’s have a look at its signature defined in PHP and what parameters it takes. see below;

e.g. :

reset($myarr);

As we can see above this method take no parameter. This parameter would be the array from which we want to access the first element. This method will return FALSE if there is no element present inside the array, simply means the array is empty. If not empty this method will return the first element from the array. Simple example for beginners to use this while programming see below;

e.g. :

$myarr = array("100", "200", "300", "400", "00");
echo reset(myarr);

These lines of code will return us the first element from the array. We are just passing the array inside the method.

2. By using the direct index

This is the most common way to get the first element form the array, it is applicable and used in any programming language to access the array element.

e.g. :

$myarr = array("100", "200", "300", "400", "00");
echo $myarr[0];

As you can see we are just passing the index of the using the ‘[]’ square brackets, but this is not useful when we have a different index for our array element or we do not know the index of the element inside array. But in a simple scenario, it will return us the first element from the array.

3. By using current() Method

we can also use current() method to get the first element from the array. This method always points out to the current element from the array, but this can be used reset() method. Let’s have a look at its method signature and what parameters does it take;

e.g. :

$myarr = array("100", "200", "300", "400", "00");
echo current(myarr);

As we can see it take only one parameter and this would be an array from which we may want to access the first element.

4. By using array_value() Method

This method is also used to get the first element from the array. This method will help us to get all the values present inside the array and after this we can directly access the first element by passing the index.

e.g. :

$myarr = array("100", "200", "300", "400", "00");
echo array_values(myarr)[0];

As you can see in this method we are passing our array, first, it will give us all the values which are present inside the array after this we can immediately access the first element from the array. In the coming section, we will see the working example to get a better understanding of the methods available in PHP.

Examples

In this example, we are using a basic approach to get the first element from an array.

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br ("Demo to get the first element from the array !! \n");
$myarr1 = array("100", "200", "300", "400", "500");
echo nl2br ($myarr1[0]."\n");
$myarr2 = array("first", "second", "third", "fourth", "five");
echo nl2br ($myarr2[0]."\n");
$myarr3 = array("hello", "to", "all", "bye", "bye");
echo nl2br ($myarr3[0]."\n");
?>
</body>
</html>

Output:

PHP Get First Element of Array

Example #2

In this example, we are using array_values methods to get the first element from the array.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br ("Demo to get the first element from the array using array_values method in PHP!! \n");
$myarr1 = array("100", "200", "300", "400", "500");
echo nl2br (array_values($myarr1)[0]."\n");
$myarr2 = array("first", "second", "third", "fourth", "five");
echo nl2br (array_values($myarr2)[0]."\n");
$myarr3 = array("hello", "to", "all", "bye", "bye");
echo nl2br (array_values($myarr3)[0]."\n");
?>
</body>
</html>

Output:

PHP Get First Element of Array

Conclusion

Like in other programming language we have most common way to get the first element from the array is by passing the index. Apart from this, we have multiple methods available in PHP to get the value as well. all these methods are in build in PHP we do not need to import or include any library for this can be used directly.

The above is the detailed content of PHP Get First Element of Array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:2D Arrays in PHPNext article:2D Arrays in PHP