Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
array
ip
communitycreator

What is the array reset() method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The reset method is used to reset the position of the internal pointer of the array to the first element and return the value.

In PHP, each array contains an internal pointerIP that points to the current element. The pointer initially points to the first element of the array.

We can move the pointer positions using methods like next, prev, etc.

Syntax


reset(array|object &$array): mixed

Return value

The reset() method returns the value. If an array is empty, then false is returned.

Code

<?php 

  $numbers = [1,2,3,4,5];

  echo "Current Value is : ". current($numbers). "\n";
  next($numbers);
  next($numbers);
  next($numbers);
  echo "Current Value is : ". current($numbers). "\n";

  echo "Moving to the start of the array : ". reset($numbers). "\n";

  echo "Current Value is : ". current($numbers). "\n";
?>

Explanation

In the code above:

  • We create a numbers array.

  • We use the current method to print the value pointed by the current internal pointer of the array.

  • We use the next method multiple times to move the pointer position one step forward and print the value pointed by the pointer.

  • We then use the reset method to reset the internal pointer position to the first element of the array and print the value.

RELATED TAGS

php
array
ip
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring