array_values
Returns all the values of an array reindexed numerically from zero, discarding the original keys.
array_values(array $array): array- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array to take the values from. It is not modified. |
Return value
arrayA new array containing the values of $array, indexed sequentially from 0.
Errors
Throws a TypeError if $array is not an array.
array_values() throws away the keys and hands back the values in order, numbered 0, 1, 2, …. It always produces a list, whatever went in.
When to reach for it
The most common reason is JSON. Any array with non-sequential keys encodes as a JSON object, not an array, so a filtered list suddenly serialises as {"1":2,"3":4} and breaks the client. array_values() is the one-call fix, and the operations that create the problem — array_filter(), array_unique(), unset(), array_diff() — all preserve keys by design.
It also matters for array_is_list() checks, for typed list-shaped values in static analysis, and for anything that iterates by index rather than with foreach.
What trips people up
It is shallow. Nested arrays keep their own keys, so a list of associative rows stays intact — which is usually what you want, but it means array_values() on the outer array will not repair inner arrays. Map over them if you need that.
It also copies. On a very large array you briefly hold both copies in memory; if the array is already a list, calling it is pure overhead, so guard with array_is_list() when that matters.
The order is the array's own insertion order, not a sort. If you wanted sorted values, sort first.
Example
When to use it
- Repack the result of array_filter so json_encode emits a JSON array instead of an object.
- Reset keys after unset() removed items from the middle of a list.
- Turn an associative lookup table into a plain list of its values for display.
Frequently asked questions
What is the difference between array_values and array_keys?
array_values returns the values reindexed from 0; array_keys returns the keys as a list. Both preserve the array's existing order, so their results line up element by element.
Why does json_encode turn my array into an object?
Because its keys are not sequential integers starting at 0, which happens after array_filter, array_unique or unset. Pass the array through array_values() before encoding.
Does array_values change the original array?
No. It returns a new array and leaves the input untouched, so assign the result: $list = array_values($arr).
More examples
Repacking after unset
unset leaves keys 0, 2, 3, so json_encode produces {"0":"a","2":"c","3":"d"}. After array_values the keys are 0, 1, 2 and the JSON is ["a","c","d"].
<?php
$items = ['a', 'b', 'c', 'd'];
unset($items[1]);
print_r($items);
print_r(array_values($items));
echo json_encode($items), ' vs ', json_encode(array_values($items)), "\n";Values and keys are complementary
array_keys and array_values split the array into two parallel lists in the same order, and array_combine puts them back together. This is the standard way to transform keys and values independently.
<?php
$stock = ['apples' => 3, 'pears' => 7];
$keys = array_keys($stock);
$vals = array_values($stock);
print_r($keys);
print_r($vals);
print_r(array_combine($keys, $vals));It only flattens the outer level
The outer keys 4 and 9 become 0 and 1, but each inner row keeps its 'id' and 'name' keys. That is exactly what a JSON array of objects needs.
<?php
$rows = [4 => ['id' => 4, 'name' => 'x'], 9 => ['id' => 9, 'name' => 'y']];
$list = array_values($rows);
print_r($list);
echo json_encode($list), "\n";
Discussion