What it is and why to use it
A PHP array is an ordered map: every value has an integer or string key. Removing an item does not automatically close gaps in integer keys. array_values() creates a new array with consecutive keys starting at zero. Use it when a result represents a list of values rather than a lookup table, especially before encoding a filtered list as JSON.
How it works
Pass the original array as the single argument. PHP visits its current iteration order, copies each value into the result, and discards its old key. It does not sort the values or change the original variable. A numerically indexed array with gaps is encoded by json_encode() as a JSON object; reindexing gives the sequential keys needed for a JSON array.
Common mistakes and limitations
Do not reindex a map whose keys carry meaning, such as product IDs or language codes. This operation is not a sorting function or a deep clone of nested objects. Reindex only after deciding that callers need positions rather than the original keys.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$list = array_values($array);Examples
Close a gap after removing a task
Remove one task, then prepare the remaining tasks as a JSON list.
<?php
declare(strict_types=1);
$tasks = [0 => 'Read', 1 => 'Watch', 2 => 'Practise'];
unset($tasks[1]);
$list = array_values($tasks);
echo json_encode($tasks, JSON_THROW_ON_ERROR), PHP_EOL;
echo json_encode($list, JSON_THROW_ON_ERROR), PHP_EOL;
- unset removes key 1, leaving keys 0 and 2. The remaining values keep their order.
- array_values builds a new array with keys 0 and 1. The first variable still has its original gap.
- The first JSON result is an object with named properties; the second is a JSON array. An API that promises a list should return the second shape.
- Expected output: {"0":"Read","2":"Practise"} ["Read","Practise"]
Extract display labels from a map
Keep a lookup map for internal use and create a separate list for display.
<?php
declare(strict_types=1);
$labels = ['php' => 'PHP', 'js' => 'JavaScript'];
$names = array_values($labels);
echo implode(', ', $names), PHP_EOL;
echo $labels['php'], PHP_EOL;
- The string keys identify languages in the original map.
- array_values discards those keys only in names. implode joins its values using a comma and space.
- The final lookup still works, demonstrating that creating a display list does not erase the source map.
- Expected output: PHP, JavaScript PHP
Best practices
- Keep identifiers as keys when they are part of the data model.
- Reindex list results deliberately before JSON encoding.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual