All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Get First Element Of Array

Last Updated : Mar 11, 2024

PHP Get First Element Of Array

In this article we will show you the solution of PHP get first element of array, an array is a homogenous collection of elements. We can get the first element of an array by using different methods using php.

In this tutorial, we are going to use two different methods :-

  • Reset() : this function in php is used to move the pointer to the first element
  • Array_key_first() : this function is used to get the key of the first element of the array

Step By Step Guide On PHP Get First Element Of Array :-

Let us see examples of two different methods to get the first element of the array

Method 1

In the example below, we are going to use reset() function.

<!DOCTYPE html&g;
<html lang = " en " &g;
<head&g;
    <meta charset = " UTF - 8" &g;
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " &g;
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " &g;
    <title&g; php get first element of array </title&g;
</head&g;
<body&g;
    <h1 style=" color : rgb(113, 221, 113) ;"&g; TALKERSCODE </h1&g;
    <h2&g; php get first element of array </h2&g;
    <?php
    $color = array('red', 'black', 'white', 'green', 'blue', 'pink') ;
    echo reset($color) ;
    ?&g;
</body&g;
</html&g;
  1. First, we write <! DOCTYPE html&g; which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
  3. As above now the <head&g; tag is used to contain information about the web page. In this tag, a <title&g; tag is used which helps us to specify a webpage title.
  4. Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
  5. Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1&g; tag used to add heading here.
  7. Now we close the HTML file with </html&g; tag
  8. we write <?php tag to write PHP within it.
  9. First create an array with some values.
  10. Using echo reset() to get the first element.
  11. ?&g; to close the php code.

Method 2

In this method, we are going to use array_key_first() function.

<!DOCTYPE html&g;
<html lang = " en " &g;
<head&g;
    <meta charset = " UTF - 8" &g;
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " &g;
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " &g;
    <title&g; php get first element of array </title&g;
</head&g;
<body&g;
    <h1 style=" color : rgb(113, 221, 113) ;"&g; TALKERSCODE </h1&g;
    <h2&g; php get first element of array </h2&g;
    <?php
$flowers = array('rose', 'tulip', 'peony', 'jasmine', 'lavender', 'lavender', 'lotus', 'daisy');
foreach ($flowers as $index =&g; $array) {
    if($index === array_key_first($flowers)) {
        echo $array ;
    }
}
?&g;
</body&g;
</html&g;
  1. First, we write <! DOCTYPE html&g; which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
  3. As above now the <head&g; tag is used to contain information about the web page. In this tag, a <title&g; tag is used which helps us to specify a webpage title.
  4. Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
  5. Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1&g; tag used to add heading here.
  7. Now we close the HTML file with </html&g; tag
  8. we write <?php tag to write PHP within it.
  9. First create an array with some values.
  10. Create the foreach loop then create an if condition within it. Using the array_key_first() method to get the value of the first key to an array then use echo to print the value of the first element.
  11. Close the php code with ?&g; tag.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to get first element of array using PHP.

I hope this article on PHP get first element of array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪