Content · Programming · PHP

PHP array_values() Function: Usage and Examples

Understand array_values() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to get all array values in a fresh zero-based indexed array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_values() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_values() in PHP?

Get all array values in a fresh zero-based indexed array.

Its syntax is:

array_values(array $array): array

Parameters used by this operation:

  • $array — The collection being examined or updated.

Keys are discarded, including meaningful string identifiers. Use it only when you intentionally need values without keys.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$scores = ["Maya" => 90, "Leo" => 85];
print_r(array_values($scores));

Output:

Array
(
    [0] => 90
    [1] => 85
)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$data = [3 => "red", 7 => "green"];
print_r(array_values($data));

Output:

Array
(
    [0] => red
    [1] => green
)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Prepare a filtered associative collection for a JSON array response

Prepare a filtered associative collection for a JSON array response. Save the code in a PHP file and adapt the sample data to your project.

<?php

$productStock = ["P-100" => 8, "P-300" => 12];
$stockValues = array_values($productStock);
print_r($stockValues);

Output:

Array
(
    [0] => 8
    [1] => 12
)

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Keys are discarded, including meaningful string identifiers. Use it only when you intentionally need values without keys.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_values()Get all array values in a fresh zero-based indexed array
Related functionsarray_keys() returns keys; array_combine() can create a key/value array.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyKeys are discarded, including meaningful string identifiers. Use it only when you intentionally need values without keys.
Choosing a related function with different behaviorarray_keys() returns keys; array_combine() can create a key/value array.

Key Takeaways

  • array_values() helps you get all array values in a fresh zero-based indexed array.
  • Keys are discarded, including meaningful string identifiers. Use it only when you intentionally need values without keys.
  • array_keys() returns keys; array_combine() can create a key/value array.
  • Use the practical example as a starting point, but validate real application inputs.

array_values() is useful when you need to get all array values in a fresh zero-based indexed array. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.