It is very common when writing code that you will have to deal with a collection of objects. It could be a set of customer names or the number of posts created by different users. Storing this data in arrays helps us to work with the whole collection by iterating through it one by one. Show
PHP comes with two different types of arrays to help you store data. You can either use simple numerical arrays or you can create associative arrays. Numerical arrays are helpful when you want to just store a list of items—for example a list of customers. Associative arrays are useful when you want to store key-value pairs like a list of customer IDs and the total value of products purchased by each one. Every now and then, you will have to access directly access the elements in the arrays you created. In this quick tip, I'll show you how to get the first or last element of an array in PHP. Get the First Element of an Array in PHPIt's easy to get the first element of a simple numerical array. Just get the element with index 0! <?php $names = ["Adam", "Monty", "Sajal", "Andrew"]; // Output — Adam echo $names[0]; ?> However, this doesn't work for an associative array. One of the most efficient ways of getting the first element of a numerical or associative array in PHP is to use the <?php $names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy']; $country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra']; // Output — Andrew echo reset($names); // Output — Ottawa echo reset($country_capitals); ?> As you can see, we did not need to know the key for the first element to get its value. All we have to do is pass the array to As I mentioned earlier, using <?php $names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy']; $country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra']; // Output — Andrew echo $names[array_key_first($names)]; // Output — Ottawa echo $country_capitals[array_key_first($country_capitals)]; ?> One thing you should keep in mind is that Get the Last Element of an Array in PHPFor a simple numerical array, you can get the last element by calculating its index from the array length. <?php $names = ["Adam", "Monty", "Sajal", "Andrew"]; // Output — Andrew echo $names[count($names)-1]; ?> However, once again, this will not work with associative arrays. You can use the <?php $names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy']; $country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra']; // Output — Roy echo end($names); // Output — Canberra echo end($country_capitals); ?> Just like <?php $names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy']; $country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra']; // Output — Roy echo $names[array_key_last($names)]; // Output — Canberra echo $country_capitals[array_key_last($country_capitals)]; ?> The Final ThoughtsThere
are many more methods to get the first or last array of an element in PHP. For example, you could use Did you find this post useful? Freelancer, Instructor I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around. How do you find the first and last value of an array?To get the first and last elements of an array, access the array at index 0 and the last index. For example, arr[0] returns the first element, whereas arr[arr. length - 1] returns the last element of the array.
How do you print the first and last element of an array in PHP?$firstEle = reset($arr); reset() rewinds array's internal pointer to the first element and returns the value of the first array element. And end() to get the last: $lastEle = end($arr);
How can we get the last element of an array in PHP?You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value.
How can we get the first element of an array in PHP?There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more.
|