Verification: a143cc29221c9be0

Php array index first element

Use the Index of Elements to Get the First Element of an Array in PHP

We know that the index of the first element in an array is 0, therefore, you can directly get the first element by accessing it through its index. The correct syntax to get the element via an index is as follows

$arrayName[0];

To get the first element, the index of the first element, 0, is enclosed in square brackets along with the array name.

Output:

The first element of the array is Rose.

Use the reset() Function to Get the First Element of an Array in PHP

The built-in function reset() sets the pointer of an array to its start value i.e the first element of the array.

reset($arrayName);

It has only one parameter. It returns the value of the first element if the array is not empty. It returns false if the array is empty.

Output:

The first element of the array is Rose.

Use the current() Function to Get the First Element of an Array in PHP

The current() function is another built-in function used to get the value in the array that the pointer is currently pointing. It can be used to get the first element of the array if there is no internal pointer in the array. The pointer points to the first element by default. The correct syntax to use this function is as follows

current($arrayName);

It only accepts one parameter $arrayName. The variable $arrayName is an array of which we want to get the first element.

Output:

The first element of the array is Rose.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.