How To Reset Array Keys In PHP

How to reset array keys in php

Arrays are a major part of PHP's programming language and as you browse open-source software, or work on private code repositories built by others, you'll soon see arrays everywhere. Manipulating these arrays of data into more meaningful ways or resetting back to an ordered list is something good to have under your belt. In this guide, we'll learn how to reset array keys in PHP, including resetting array values, and look at the two different types of arrays you might come across all using PHP's built-in functions. All the points will be accompanied by PHP code examples.

What are PHP arrays?

Arrays are a term used to store data in a structured way. Nearly all programming languages use them, and PHP is no different. Arrays are a great way to store, edit, and ultimately read out data in a pre-defined way. Because arrays work in a particular way, (syntax), it's easy for developers to read other people's code as it's essentially standarised. This is also true for a lot of other functions in PHP, particularly those following a coding standard such as PSR-12. Arrays can sort all types of different formats from, strings, and booleans to integers. You can even nest other arrays inside of others, called a multidimensional array. You also have associative arrays, which are arrays that have a string for each key with its own value. Understanding the difference between a multi-dimensional and associative array are important stypes for working arrays when you require to reset their keys.

# A simple, flat array
$array = [
 0 => 'test'
];

Multidimensional Array in PHP

A multidimensional array in PHP is an array that contains multiple arrays within it or nested arrays. This takes the standard flat array of just keys and values into a more complex array of arrays, which all again contain keys and values.

# A multidimensional array
$array = [
 0 => [
 0 => 'test'
 ]
];

Associative Array in PHP

An associative array is one that instead of the default int numbering for each key, instead each key is a unique string.

# A simple, associative array
$array = [
 'php' => 'is great'
];

Because the keys are strings and not int orders, calling the zero index would result in a PHP warning, the undefined array key 0.

# Calling the zero index of an associative array, would fail
$array = [
 'php' => 'is great'
];

# Triggers: Warning: Undefined array key 0
var_dump($array[0]);

Resetting PHP Array Keys In PHP

In PHP, there are times when you need to adjust the order or reset keys in an array. In PHP that's nice and straightforward. To reset an array in PHP, we can use the function, 'array_values'. Based on int keys, PHP will reset the order of the keys, so that the lowest number is first. Let's look at the example below on how to reset PHP array keys.

# Unordered PHP array
$array = [
 3 => 'test',
 1 => 'test',
 2 => 'test',
];

# Will reset keys back too: 1, 2, and 3
array_values($array);

Array_values function also works when you have a mismatch of array key values. Let's take a look at another example, where some keys are strings, whilst others are ints.

# Unordered PHP array
$array = [
 3 => 'C',
 1 => 'A',
 2 => 'B',
];

# Now we've inserted another key, this time it is a mismatch of types.
$array['php'] = 'Test';

# It will still reset keys back: 0, 1, 2, and 3
array_values($array);

Conclusion

When you require the mismatch or unordered array of keys to be reset back in order, using the array_values function in PHP is a great and easy way to do just that.

  • Use array_values to reset the array keys in PHP
  • When resetting the keys, make sure you don't require the string keys in an associative array, as they get reset to the array index

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all