Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_values

Returns the values of an array with keys numbered from zero.

Syntax

array_values(array $array): array

Parameters

ParameterTypeDescription
$arrayarrayThe source array.

Return value

arrayA list of the values.

Examples

The values of an associative array:

<?php
 
echo implode(', ', array_values(['name' => 'Anna', 'city' => 'London']));

Output:

Anna, London

Renumbering after filtering:

<?php
 
$big = array_filter([5, 12, 8, 20], fn ($n) => $n > 10);
 
echo json_encode($big), "\n";
echo json_encode(array_values($big));

Output:

{"1":12,"3":20}
[12,20]

Notes

array_filter() and array_unique() keep the original keys. array_values() removes the gaps in the numbering.

See also

Related recipes

In the tutorial

  • Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.

Thanks for reading!

Donate to the site — we work hard for you.