Current Location: Home> Latest Articles> How to Use PHP's array_values Function to Extract Values from an Associative Array While Ignoring Keys? A Detailed Explanation

How to Use PHP's array_values Function to Extract Values from an Associative Array While Ignoring Keys? A Detailed Explanation

gitbox 2025-06-08

In PHP, arrays are a crucial data structure. Besides indexed arrays, associative arrays are also widely used. In associative arrays, the keys are strings or integers, while the values can be any type of variable. Often, we might only care about the values in the array and not the keys. This is where the array_values() function comes in handy.

This article will explain in detail how to use the array_values() function to extract all values from an associative array while ignoring the keys.


Introduction to the array_values() Function

array_values() is a built-in PHP array function used to return all values in an array as a new indexed array. This function does not preserve the original keys; instead, it reindexes the array numerically.

Function Prototype

array_values(array $array): array
  • $array: The input associative array.

  • Return value: A new array containing all the values from $array, with the keys reindexed numerically.


Example: How to Extract Values from an Associative Array

Suppose we have an associative array as shown below:

$person = [
    "name" => "John",
    "age" => 30,
    "city" => "New York"
];

If we're only interested in the values of the array, we can use array_values() to extract them into a new array:

$values = array_values($person);
print_r($values);

Output:

Array
(
    [0] => John
    [1] => 30
    [2] => New York
)

As shown, array_values() returns a new array containing all the values from the original array, with keys reindexed as numbers.


Real-World Application of Ignoring Keys

Sometimes, when working with associative arrays, we only care about the values and not the original keys. By using array_values(), we can easily ignore these keys and retrieve just the values.

For example, suppose we have an array storing login information for several users, and we only want to extract the list of usernames:

$user_logins = [
    "user1" => "john_doe",
    "user2" => "jane_smith",
    "user3" => "alice_walker"
];
<p>$usernames = array_values($user_logins);<br>
print_r($usernames);<br>

Output:

Array
(
    [0] => john_doe
    [1] => jane_smith
    [2] => alice_walker
)

In this example, we used array_values() to extract all the usernames while ignoring the keys in the original array.


Why Use array_values()?

  1. Simplified Processing: When we only care about the values in an array and not the keys, using array_values() simplifies the code.

  2. Data Extraction: Especially when extracting data, if we need a clean indexed array, array_values() helps reindex the array and avoids key-related interference.

  3. Data Consistency: By converting all keys into numeric indexes, the resulting data structure is more consistent and suitable for passing to other functions or for generating forms, JSON responses, etc.


Conclusion

PHP’s array_values() function offers a simple and efficient way to extract values from an associative array while ignoring the keys. Whether you're processing data or performing specific operations, this function proves to be a very useful tool.

If you're working with an array that includes keys but you're only interested in the values, array_values() is undoubtedly a practical solution.