PHP Func Array Values: A Comprehensive Guide
Welcome to our comprehensive guide to PHP's array_values() function. In this guide, we will discuss what the array_values() function does, how it works, and
The array_values() function returns all the values of an array, re-indexed with consecutive integer keys starting from 0. It is one of the simplest yet most-used array helpers in PHP: whenever an array has gaps in its keys, or string keys you no longer need, array_values() gives you back a clean, list-style array. This page covers its syntax, when to reach for it, how it compares to related functions, and runnable examples.
What is the array_values() function?
array_values() is a built-in PHP function that takes an array and returns a new array containing all of its values. The original array is left unchanged. The returned array always uses sequential integer keys (0, 1, 2, …), and the original keys — whether strings or non-sequential integers — are discarded. The relative order of the values is preserved.
Syntax and return value
array_values(array $array): array$array— the input array whose values you want to extract.- Returns an indexed array of all the values, re-keyed from
0.
How does array_values() work?
The syntax is straightforward. Here is a basic example:
The output of this code would be:
Array
(
[0] => apple
[1] => banana
[2] => cherry
)As you can see, the array_values() function takes an input array $input and returns a new array $output containing all the values of $input, with new numeric keys starting from zero. The original keys of the input array are discarded.
Why Use array_values()?
The array_values() function can be useful in a variety of situations. For example, if you have an associative array with non-sequential keys, you may want to use array_values() to create a new array with sequential numeric keys. This can be helpful if you need to perform operations on the values of the original array that require sequential keys, such as sorting or searching.
Another use case for array_values() is when you need to remove the keys from an array, but preserve the order of the values. If you simply use array_values() on the original array, the new array will have the same order as the original array, but with numeric keys starting from zero.
Comparing array_values() to Other PHP Functions
In addition to array_values(), there are other PHP functions that perform similar tasks. Here's a brief comparison:
array_keys(): Returns an array of all the keys in the input array — the mirror image ofarray_values().array_flip(): Exchanges all keys with their associated values.array_combine(): Creates a new array using one array for keys and another for values.array_column(): Returns the values from a single column of a multi-dimensional array.
While these functions are useful in their own right, they have different use cases. array_keys() and array_flip() work with the keys of an array, while array_combine() combines two arrays into one. To learn more about arrays in general, see the PHP Arrays chapter.
Best Practices for Using array_values()
Here are some tips and best practices for using array_values() effectively:
- Be aware that
array_values()discards the original keys of the input array. If you need to preserve the keys, use a different function, such asarray_keys(). - If you have an array with non-sequential keys, use
array_values()to create a new array with sequential keys before performing operations that require sequential keys. - Use
array_values()to remove the keys from an array while preserving the order of the values. - When working with large arrays, be aware that creating a new array with
array_values()can consume a significant amount of memory. In these cases, consider using alternative methods that do not require creating a new array. - Use good naming conventions for your variables to make your code more readable and understandable.
Practical Examples of Using array_values()
Now let's take a look at some practical examples of using array_values() in PHP code.
Example 1: Removing Non-Numeric Keys from an Array
Suppose you have an associative array with non-numeric keys, and you want to remove the keys and create a new array with sequential numeric keys. Here's how you can do it using array_values():
PHP array_values function usage with associative array
The output of this code would be:
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => date
)As you can see, array_values() has created a new array with sequential numeric keys, while discarding the original non-numeric keys.
Example 2: Re-indexing an Array After Filtering
This is the most common real-world use of array_values(). Functions like array_filter() preserve the original keys, so removing elements leaves gaps in the numbering. Calling array_values() afterward gives you a clean, gap-free list:
PHP re-indexing an array after array_filter()
The output of this code would be:
Array
(
[2] => 10
[3] => 15
[4] => 20
)
Array
(
[0] => 10
[1] => 15
[2] => 20
)Notice how array_filter() keeps the original keys 2, 3, and 4, while array_values() resets them to 0, 1, and 2. This matters when you later JSON-encode the array (gapped integer keys become a JSON object instead of an array) or rely on positional access.
Conclusion
array_values() returns a new, re-indexed array containing all the values of the input array, discarding the original keys. Reach for it whenever you need a clean list-style array — most often after filtering, removing, or sorting operations that leave non-sequential keys behind. Remember that it never modifies the original array, and that for very large arrays the copy it creates has a memory cost worth keeping in mind.