Reset keys of array elements using PHP ?

To reset keys of array elements in PHP, you can use the array_values() function. This function returns a new indexed array with numeric keys starting from 0, preserving the original order of values while discarding the original keys.

Syntax

array_values(array $array)

Parameters

$array − The input array whose keys need to be reset.

Return Value

Returns an indexed array with numeric keys starting from 0.

Example 1: Resetting String Keys

Here's how to reset associative array keys to numeric indexes ?

<?php
    $arr = array("p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");
    echo "Original array:<br>";
    var_dump($arr);
    
    $res = array_values($arr);
    echo "\nAfter resetting keys:<br>";
    var_dump($res);
?>
Original array:
array(4) {
  ["p"]=>
  string(3) "150"
  ["q"]=>
  string(3) "100"
  ["r"]=>
  string(3) "120"
  ["s"]=>
  string(3) "110"
}

After resetting keys:
array(4) {
  [0]=>
  string(3) "150"
  [1]=>
  string(3) "100"
  [2]=>
  string(3) "120"
  [3]=>
  string(3) "110"
}

Example 2: Resetting Numeric Keys

This example shows how non-sequential numeric keys are reset to start from 0 ?

<?php
    $arr = array(8=>"Ben", 4=>"Kevin", 7=>"Mark", 3=>"Hanks");
    echo "Original array:<br>";
    var_dump($arr);
    
    $res = array_values($arr);
    echo "\nAfter resetting keys:<br>";
    var_dump($res);
?>
Original array:
array(4) {
  [8]=>
  string(3) "Ben"
  [4]=>
  string(5) "Kevin"
  [7]=>
  string(4) "Mark"
  [3]=>
  string(5) "Hanks"
}

After resetting keys:
array(4) {
  [0]=>
  string(3) "Ben"
  [1]=>
  string(5) "Kevin"
  [2]=>
  string(4) "Mark"
  [3]=>
  string(5) "Hanks"
}

Conclusion

The array_values() function is the most efficient way to reset array keys in PHP. It creates a new indexed array starting from 0 while preserving the original order and values of the elements.

Updated on: 2026-03-15T08:24:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements