Signature
array_values(array $array): arrayPurpose
array_values() takes an array and returns a new array that holds the same values in the same order, but with fresh integer keys numbered 0, 1, 2, .... Whatever keys the input had, whether strings, sparse integers or a mix, are thrown away. Think of it as "forget the labels, keep the items in line".
The function is most useful when the keys carry no meaning for the next step, or when gaps in the numbering would cause trouble. Typical cases are the result of a filter, a list after removing entries with unset(), or data that must be serialized as a JSON array rather than a JSON object.
Signature
Parameters and defaults
$array: the input array. It is required and has no default. The input itself is never modified, because PHP passes arrays by value and the function builds a separate result.
Return value and failure cases
The function returns a list: an array whose keys are exactly 0 to count - 1 in order. An empty input gives an empty array.
Because the parameter is declared as array, passing anything else, such as a string, null or an object, throws a TypeError. The function does not return false on failure, so there is nothing to check after the call.
Example 1: dropping string keys
The first example uses an associative array. Only the values survive, in their original order.
$sizes = ["size" => "XL", "color" => "gold"];
echo json_encode(array_values($sizes), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["XL","gold"]
Example 2: repairing gaps after filtering
array_filter() keeps the original keys of the items it lets through. That leaves holes in the numbering, and a JSON encoder will then treat the array as an object. Wrapping the result in array_values() closes the gaps.
$numbers = [1, 2, 3, 4, 5, 6];
$even = array_filter($numbers, fn(int $n): bool => $n % 2 === 0);
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode($even, $flags), "\n";
echo json_encode(array_values($even), $flags), "\n";{"1":2,"3":4,"5":6}
[2,4,6]
The first line shows the keys 1, 3 and 5 surviving, so the output is an object. The second line shows a clean list.
Example 3: order, duplicates and nesting
The function never sorts and never removes duplicates. It also works only on the top level, so nested arrays are carried over unchanged.
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
$scores = [10 => "b", 5 => "a", "x" => "b"];
$values = array_values($scores);
echo json_encode($values, $flags), "\n";
echo count($values), "\n";
$nested = ["a" => ["k" => 1], "b" => ["m" => 2]];
echo json_encode(array_values($nested), $flags), "\n";["b","a","b"]
3
[{"k":1},{"m":2}]
The values come out in insertion order (b, a, b), not in key order. The inner arrays keep their own string keys.
Edge cases
- Empty array:
array_values([])returns[]. - Already a list: the result is equal to the input. The call is harmless, but it creates no useful change.
- Insertion order wins: PHP arrays are ordered maps, so the output follows the order in which elements were added, not the numeric or alphabetical order of the keys.
- Key information is lost: once the keys are gone you cannot recover them from the result. If you need both, call
array_keys()first or keep the original array. - Only one level: use a recursive helper of your own if nested arrays must be renumbered as well.
Common mistakes
- Expecting the input to change.
array_values($items);on its own line does nothing visible. Assign the result:$items = array_values($items);. - Forgetting it after
unset(). Removing an element leaves a gap, so$list[count($list) - 1]may point at a missing key. Reindex first. - Using it when keys matter. Calling it on a map such as
["id" => 7, "name" => "Ada"]destroys the meaning of each value. - Assuming it deduplicates. Repeated values stay repeated. Use
array_unique()for that, and thenarray_values()if you want a tidy list. - Passing a non-array. A
nullfrom an earlier lookup will raise aTypeError, so make sure the input really is an array.
Related functions
array_keys()returns the keys instead of the values.array_combine()builds an array from one list of keys and one list of values, which is the opposite direction of splitting them.array_filter()removes elements but keeps keys, so it is often followed byarray_values().array_merge()renumbers integer keys too, but it is meant for combining arrays.array_slice()can renumber integer keys as it extracts a portion.array_is_list()tests whether an array already has sequential keys starting at zero.
Recap
array_values() returns a new, zero-based list containing every value of the input in the same order. It ignores and discards keys, keeps duplicates, changes only the top level and throws a TypeError for non-array input. Reach for it after filtering or unsetting, or before encoding data that should become a JSON array.
Tested examples
$sizes = ["size" => "XL", "color" => "gold"];
echo json_encode(array_values($sizes), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["XL","gold"]
$numbers = [1, 2, 3, 4, 5, 6];
$even = array_filter($numbers, fn(int $n): bool => $n % 2 === 0);
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode($even, $flags), "\n";
echo json_encode(array_values($even), $flags), "\n";
{"1":2,"3":4,"5":6}
[2,4,6]
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
$scores = [10 => "b", 5 => "a", "x" => "b"];
$values = array_values($scores);
echo json_encode($values, $flags), "\n";
echo count($values), "\n";
$nested = ["a" => ["k" => 1], "b" => ["m" => 2]];
echo json_encode(array_values($nested), $flags), "\n";
["b","a","b"]
3
[{"k":1},{"m":2}]