• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get First Element of Array in php

Get First Element of Array in php

March 14, 2022 Leave a Comment

To get the first element of an array in php, the easiest way is by accessing the first element directly.

$array = array(1, 2, 3, 4, 5);

echo $array[0];

// Output:
1

Another way to get the first element of an array is with the current() function.

$array = array(1, 2, 3, 4, 5);

echo current($array);

// Output:
1

A third way to get the first element of an array is with the reset() function.

$array = array(1, 2, 3, 4, 5);

echo reset($array);

// Output:
1

One final way to get the first element of an array is with the php array_values() function.

$array = array(1, 2, 3, 4, 5);

echo array_values($array)[0];

// Output:
1

When working with arrays in php, it can be valuable to be able to easily filter and get only specific values from your array.

One such situation is when you want to get the first element from a php array.

There are a number of ways to get the first element from a php array, and the easiest way is to access the first element directly by accessing the ‘0’ position.

Below is an example of how to print the first element of a php array.

$array = array(1, 2, 3, 4, 5);

echo $array[0];

// Output:
1

If you are looking to get the last element of a php array, the easiest way is with the end() function.

$array = array(1, 2, 3, 4, 5);

echo end($array);

// Output:
5

Getting the First Element of an Array in php with current()

Another way that you can get the first element of an array in php is with the current() function.

current() returns the value of the element in an array which the internal pointer is currently pointing to. Usually, the current elemnt is the first inserted item into the array.

If we use the current() function on an array, we get the first element of the array.

Below is how to use current() to get the first element of the array.

$array = array(1, 2, 3, 4, 5);

echo current($array);

// Output:
1

Getting the First Element of an Array in php with reset()

Another way that you can get the first element of an array in php is with the reset() function.

reset() is used to move any array’s internal pointer to the first element of that array. The reset() function resets the internal pointer to point to the first element of the array.

If we use the reset() function on an array, we get the first element of the array.

Below is how to use reset() to get the first element of the array.

$array = array(1, 2, 3, 4, 5);

echo reset($array);

// Output:
1

Get the First Element of an Array in php with array_values()

One final way that you can get the first element of an array in php is with the array_values() function.

array_values() creates a new array with just the values of the passed array. Then, after getting just the array values, we can access the first element.

Below is how to use array_values() to get the first element of the array.

$array = array(1, 2, 3, 4, 5);

echo array_values($array)[0];

// Output:
1

How to Get the First N Elements of a php Array

If you are looking to get more than just the first element, we can get the first n elements of a php array with the array_slice() function.

You can pass php array_slice() function a starting position and ending position, and get the elements in the array between those two positions.

To get the first n elements of an array in php, pass ‘0’ and ‘n’ to array_slice().

Below is a simple example of how to get the first three elements of an array using php.

$array = array(1, 2, 3, 4, 5);

echo var_dump(array_slice($array, 0, 3));

// Output:
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Hopefully this article has been useful for you to learn how to get the first element of an array when using php.

Other Articles You'll Also Like:

  • 1.  php Rename File – Renaming Files and Directories Easily
  • 2.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 3.  php Move File from One Directory to Another Folder
  • 4.  php array_slice() – Get Slice of Elements from Array
  • 5.  php property_exists() – Check if Property Exists in Class or Object
  • 6.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 7.  php atan – Find Arctangent and Inverse Tangent of Number
  • 8.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 9.  Using strtolower() in php to Convert String to Lowercase
  • 10.  php Absolute Value with abs Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy