PHP reset() Function
The PHP reset() function rewinds array's internal pointer to the first element and returns the value of the first array element.
Syntax
reset(array)
Parameters
array |
Required. Specify the input array. |
Return Value
Returns the value of the first array element, or false if the array is empty.
Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.
Note: The return value for an empty array is indistinguishable from the return value of an array which has a bool false first element. To check the value of the first element of an array which may contain false elements, first check the count() of elements in the array, or check if key() is not null, after that call reset() function.
Exceptions
NA.
Example:
The example below shows the usage of reset() function.
<?php $Arr = array(10, 20, "Hello", "World"); echo current($Arr)."\n"; //prints 10 echo next($Arr)."\n"; //prints 20 echo next($Arr)."\n"; //prints Hello echo current($Arr)."\n"; //prints Hello echo reset($Arr)."\n"; //prints 10 echo current($Arr)."\n"; //prints 10 echo "\n"; $Arr1 = array(); var_dump(reset($Arr1)); //bool(false) $Arr2 = array(array()); var_dump(reset($Arr2)); //array(0) { } ?>
The output of the above code will be:
10 20 Hello Hello 10 10 bool(false) array(0) { }
Example:
Consider one more example where the reset() function is used with an associative array.
<?php $Arr = array(10=>"Red", 20=>"Green", 30=>"Blue", 40=>"Black", 50=>"White"); echo current($Arr)."\n"; //prints Red echo next($Arr)."\n"; //prints Green echo next($Arr)."\n"; //prints Blue echo current($Arr)."\n"; //prints Blue echo reset($Arr)."\n"; //prints Red echo current($Arr)."\n"; //prints Red ?>
The output of the above code will be:
Red Green Blue Blue Red Red
❮ PHP Array Reference