PHP array_values()

JayRKyd95's avatar
Published Oct 7, 2023
Contribute to Docs

The array_values() function returns all the values from an array in numerical order.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

array_values(array)

Note: The array that is returned will have numeric keys that start from 0 and increase by 1.

Example

The example shown below creates an array called $vehicle with three key-value pairs. It then uses the array_values() function to extract the array’s values.

<?php
$vehicle = array("car" => "BMW", "color" => "gold", "model" => "X5");
print_r(array_values($vehicle));
?>

This results in the following output printed to the console.

Array
(
[0] => BMW
[1] => gold
[2] => X5
)

Codebyte Example

This code example is runnable and demonstrates how values can be returned using the array_values() function:

Code
Output

All contributors

Contribute to Docs

Learn PHP on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours