{"id":106,"date":"2019-09-16T16:28:22","date_gmt":"2019-09-16T16:28:22","guid":{"rendered":"https:\/\/tutorialio.com\/?p=106"},"modified":"2025-11-04T10:31:59","modified_gmt":"2025-11-04T10:31:59","slug":"get-the-first-element-of-an-array","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/get-the-first-element-of-an-array\/","title":{"rendered":"How Can I Get the First Element of an Array in PHP?"},"content":{"rendered":"<p>Getting the first element of an array seems like a simple thing to do. For simple arrays with sequential numeric keys it is actually very easy. However, the problem is in getting the first element of an array without any knowledge of its keys. In this article, we will go over six different methods that can be used to get the first element of any array with or without the help of its keys. Here are the three arrays that we will use:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 12 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n<\/code><\/pre>\n<p>I have used the short array syntax in the code above which replaces <code>array(your, elements)<\/code> with <code>[your, elements]<\/code>.<\/p>\n<h2 id=\"using-reset-to-get-the-first-element-of-an-array\">Using reset() to get the First Element of an Array.<\/h2>\n<p>The first thing that might come to a beginner\u2019s mind when getting the first element of an array is to use <code>$array_name[0]<\/code>. However, this solution only works when the array actually has an element with key 0. Things get complicated when you don\u2019t know the keys of an array.<\/p>\n<p>In such cases, you can use the <a href=\"..\/array\/reset-function.php\"><code>reset()<\/code> function in PHP<\/a> to get the first element of an array. The function <code>reset()<\/code> does two things. It sets the internal pointer of given array to its first element and it returns the value of the first array element. This will also work on associative arrays as shown in the example below:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 12 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\n\/\/ Output \u2014 Amanda\necho reset($people);\n\n\/\/ Output \u2014 Amanda\necho reset($positions);\n\n\/\/ Output \u2014 Peter\necho reset($friends);\n<\/code><\/pre>\n<p>You might have observed that the output in third case it not \u2018Amanda\u2019. It is \u2018Peter\u2019. This is because \u2018Amanda\u2019 is a key in the third array and <code>reset()<\/code> returns the value of the first element. This value is \u2018Peter\u2019 in our case.<\/p>\n<p>Keep in mind that <code>reset()<\/code> will reset the internal pointer of an array to its first element. In a way, this changes the original array. If you are using <code>reset()<\/code> inside a loop that relies on the internal array pointer, you will get unexpected results. Also remember that <code>reset()<\/code> will return FALSE if the provided array is empty.<\/p>\n<p>Once you have the value, you can also get the first key of an array using the <a href=\"https:\/\/tutorialio.com\/php\/array\/key-function.php\"><code>key()<\/code> function in PHP<\/a>. This function gives you the key from an array at the current location of the internal pointer. This means that you will have to use this function <strong>after<\/strong> you have called <code>reset()<\/code>. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\n\/\/ Output \u2014 Peter\necho reset($friends);\n\n\/\/ Output \u2014 Amanda\necho key($friends);\n<\/code><\/pre>\n<h2 id=\"using-list-to-get-the-first-element-of-an-array\">Using list() to get the First Element of an Array<\/h2>\n<p>You can also use the <a href=\"..\/array\/list-function.php\"><code>list()<\/code> function in PHP<\/a> to assign the value of first, second or any other element from an array to given variables. However, you have to keep in mind that <code>list()<\/code> only works when one of the keys in the array is 0 either implicitly or explicitly. In other cases, you will get the \u2018Undefined offset: 0\u2019 error. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 12 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\nlist($first_elem) = $people;\n\/\/ Output \u2014 Amanda\necho $first_elem;\n\n\/\/ Notice: Undefined offset: 0\nlist($first_elem) = $positions;\n\n\/\/ Notice: Undefined offset: 0\nlist($first_elem) = $friends;\n<\/code><\/pre>\n<p>Another issue is that the first element of an array is not always going to have its key as 0. However, list will always give you value of that element from an array whose key is zero. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n\nlist($first_elem) = $positions;\n\/\/ Output \u2014 Monica\necho $first_elem;\n<\/code><\/pre>\n<p>One solution to this problem is using the <a href=\"..\/array\/array-values-function.php\"><code>array_values()<\/code> function in PHP<\/a>. This will return all the values of given array as an array. This way all the elements of the new array will have sequential numeric keys starting from zero. Using <code>list()<\/code> on the returned array will then give us our elements.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\nlist($first_elem) = array_values($people);\n\/\/ Output \u2014 Amanda\necho $first_elem;\n\nlist($first_elem) = array_values($positions);\n\/\/ Output \u2014 Amanda\necho $first_elem;\n\nlist($first_elem) = array_values($friends);\n\/\/ Output \u2014 Peter\necho $first_elem;\n<\/code><\/pre>\n<p>One advantage of using <code>list()<\/code> is that you can perform multiple assignments at once and skip values that you don\u2019t want to assign. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n\nlist($a, $b, , , $e) = array_values($positions);\n\/\/ Output \u2014 Amanda John Harry\necho $a.\" \".$b.\" \".$e.\"\\n\";\n<\/code><\/pre>\n<h2 id=\"using-arrayvalues-to-get-the-first-element-of-an-array\">Using array_values() to get the First Element of an Array<\/h2>\n<p>As I have mentioned in the previous section of this article, you can use <code>array_values()<\/code> to return an indexed array with all the values of your original array.  Once you have the indexed array, you can just directly use <code>$indexed_array[$index]<\/code> to get the element at any given index. You no longer have to use the <code>list()<\/code> function.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\n\/\/ Output \u2014 Amanda\necho array_values($people)[0];\n\n\/\/ Output \u2014 Amanda\necho array_values($positions)[0];\n\n\/\/ Output \u2014 Peter\necho array_values($friends)[0];\n\n\/\/ Output \u2014 Monica\necho array_values($positions)[3];\n<\/code><\/pre>\n<p>In fact, as you saw in the above example, you can use this method to get the array elements at any given index.<\/p>\n<h2 id=\"using-arraypop-to-get-the-first-element-of-an-array\">Using array_pop() to get the First Element of an Array<\/h2>\n<p>If you know about the <a href=\"..\/array\/array-pop-function.php\"><code>array_pop()<\/code> function in PHP<\/a>, you might be thinking that it is used to pop the last element of an array. You are right! However, we can also use <code>array_pop()<\/code> in combination with the <a href=\"..\/array\/array-reverse-function.php\"><code>array_reverse()<\/code> function<\/a> in order to first reverse the array and the pop the last element. This last element was originally the first element of the array.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];\n$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n$friends = ['Amanda' =&gt; 'Peter', 'John' =&gt; 'Jane',\n            'Sally' =&gt; 'Molly', 'Monica' =&gt; 'Ross',\n            'Harry' =&gt; 'Hannah'];\n\n\/\/ Output \u2014 Amanda\necho array_pop(array_reverse($people));\n\n\/\/ Output \u2014 Amanda\necho array_pop(array_reverse($positions));\n\n\/\/ Output \u2014 Peter\necho array_pop(array_reverse($friends));\n<\/code><\/pre>\n<p>You might get a warning similar to \u2018Only variables should be passed by reference\u2019. You can get rid of it by using the following code:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$positions = [ 21 =&gt; 'Amanda', 14 =&gt; 'John', 32 =&gt; 'Sally', 0 =&gt; 'Monica', 3 =&gt; 'Harry'];\n\n$reversed = array_reverse($positions);\n\n\/\/ Output \u2014 Amanda\necho array_pop($reversed);\n<\/code><\/pre>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>The simplest way to get the first element from an array is to use the <code>reset()<\/code> function in PHP. This function sets the internal pointer of given array to its first element and it returns the value of that element.<\/li>\n<li>You can also use the <code>list()<\/code> function to get the first element of an array. The only problem with this technique is that one of the array keys must be 0. However, you can use <code>list()<\/code> to quickly assign the array values to multiple variables at once.<\/li>\n<li>If none of the array keys is zero, you can use the <code>array_values()<\/code> function to re-index the array. This will assign numeric keys to all array elements sequentially. After that, you can get the first element from the array using <code>$reindexed[0]<\/code>.<\/li>\n<li>Another interesting technique to get the first element of an array involves the use of PHP <code>array_pop()<\/code> function. This function returns the last element of an array. However, you can first use <code>array_reverse()<\/code> to reverse the original array so that the first element becomes the last. Once the array has been reversed, you can use <code>array_pop()<\/code> on it.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the first element of an array in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting the first element of an array seems like a simple thing to do. For simple arrays with sequential numeric keys it is actually very easy. However, the problem is in getting the first element of an array without any knowledge of its keys. In this article, we will go over six different methods that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":10245,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions\/10245"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}