PHP array_values() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_values() returns all the values of an array with fresh numeric keys starting from 0.

It throws away the old keys. This is the easiest way to turn an associative array into a plain list, or to tidy up gaps left by unset() or array_filter().

Syntax

Call array_values() with the values you need. Start with this line.

PHP
$list = array_values($data);
CallWhat it does
array_values(["a" => "x", "b" => "y"])Returns ["x", "y"].
array_values($array)Renumbers keys to 0, 1, 2...
array_values(array_filter($items))Filters first, then resets the keys.

array_values() is often combined with array_filter(), array_unique() and unset(), since these can leave gaps in the keys.

Library declarations
PHP
array_values(array $array): array

Parameters

ParameterDescription
arrayRequired. The array to take values from.

Return Value

Return ValueDescription
arrayA new indexed array holding all the values in their original order.

PHP Examples

Example 1 — Drop the Keys

Turn an associative array into a simple list.

PHP
<?php
$person = ["name" => "Ann", "city" => "Paris"];
$list = array_values($person);

echo json_encode($list), PHP_EOL;

How It Works

  1. The array has the string keys "name" and "city".
  2. array_values() collects just the values in order.
  3. New keys 0 and 1 are assigned.
  4. json_encode() prints the list as a JSON array.

Example 2 — Fix Gaps After unset()

Removing an item leaves a hole in the keys. array_values() closes it.

PHP
<?php
$nums = [10, 20, 30, 40];
unset($nums[1]);

echo json_encode($nums), PHP_EOL;
echo json_encode(array_values($nums)), PHP_EOL;

How It Works

  1. unset($nums[1]) removes the value 20 but does not renumber the keys.
  2. The keys are now 0, 2 and 3, so json_encode() writes an object.
  3. array_values() assigns the keys 0, 1 and 2.
  4. The second line shows a clean JSON list.

Example 3 — Clean Up After array_filter()

array_filter() keeps the original keys. Reset them with array_values().

PHP
<?php
$ages = [12, 25, 17, 30];
$adults = array_filter($ages, fn($age) => $age >= 18);

echo json_encode($adults), PHP_EOL;
echo json_encode(array_values($adults)), PHP_EOL;

How It Works

  1. The filter keeps ages 18 and over, which are 25 and 30.
  2. They keep their original keys 1 and 3.
  3. array_values() renumbers them as 0 and 1.
  4. The second output is a simple list you can loop over with a counter.

Frequently Asked Questions

It returns a new array containing all the values of the input array, indexed from 0.
Use array_values($array). It renumbers the keys from 0 while keeping the order.
No. It returns a new array. Assign the result to a variable to use it.
array_values() returns the values. array_keys() returns the keys.
array_filter() keeps the original keys, which can leave gaps. array_values() closes the gaps.

Next: array_walk()

array_walk() runs a function on every item of an array.

array_walk() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful