php8.5
Home/ Manual/ array / functions/ reset

reset

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Set the internal pointer of an array to its first element

Description

reset(array|object $array): mixed

reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

Parameters

array

The input array.

Return Values

Returns the value of the first array element, or false if the array is empty.

Falseproblem

Changelog

VersionDescription

Examples

reset() example

php
<?php

$array = array('step one', 'step two', 'step three', 'step four');

// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"

// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"

// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"

?>

Notes

Note

The return value for an empty array is indistinguishable from the return value in case of an array which has a bool false first element. To properly check the value of the first element of an array which may contain false elements, first check the count() of the array, or check that key() is not null, after calling reset().

See Also

Source: reference/array/functions/reset.xml · from the official PHP manual (php/doc-en)