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;
- 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.
- Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
- Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1&g; tag used to add heading here.
- Now we close the HTML file with </html&g; tag
- we write <?php tag to write PHP within it.
- First create an array with some values.
- Using echo reset() to get the first element.
- ?&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;
- 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.
- Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
- Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1&g; tag used to add heading here.
- Now we close the HTML file with </html&g; tag
- we write <?php tag to write PHP within it.
- First create an array with some values.
- 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.
- 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.