Skip to main content

How to Reset Array Keys in PHP?

In PHP, arrays are a powerful data type that can be used to store any number of values in the same variable. It is often necessary to reset an array key when using it in different portions of code. This article will show you how to do so.

Resetting an array’s key will force all keys to start from 0 and increase 1 by 1 again.

$japaneseNames = [
        '6' => 'Oda',
        '54' => 'Mitsuru',
        '17' => 'Kenichi',
        '1' => 'Saitama'
    ];

$japaneseNames = array_values($japaneseNames); var_dump($japaneseNames);

Output:

array(4) {
  [0]=>
  string(3) "Oda"
  [1]=>
  string(7) "Mitsuru"
  [2]=>
  string(7) "Kenichi"
  [3]=>
  string(7) "Saitama"
}
  • array_values() returns all the values from the array and arranges them in numerical order.

By continuing to use the site, you agree to the use of cookies.