Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. array_keys() / array_values() / array_column()

array_keys() / array_values() / array_column() Since: PHP 4(2000)

Functions for retrieving only the keys or values of an array, or for swapping keys and values. Frequently used when working with associative arrays.

Syntax

// Returns all keys of an array.
array_keys(array, search_value, strict);

// Returns all values of an array.
array_values(array);

// Extracts the values of a specific column from a multidimensional array.
array_column(array, column_key, index_key);

// Swaps the keys and values of an array.
array_flip(array);

Function List

FunctionDescription
array_keys($array, $value, $strict)Returns all keys of an array. If the second argument is specified, returns only the keys associated with that value.
array_values($array)Returns all values of an array. The keys are re-indexed starting from 0.
array_column($array, $column_key, $index_key)Extracts the values of a specified column from a multidimensional array and returns them as an array. Added in PHP 5.5.
array_flip($array)Swaps the keys and values of an array. If values are duplicated, only the key of the last value is kept.

Sample Code

<?php
$product = ['name' => 'Laptop', 'price' => 98000, 'stock' => 15];

// Get all keys.
$keys = array_keys($product);
print_r($keys); // Returns a list of keys: 'name', 'price', 'stock'.

// Get all values.
$values = array_values($product);
print_r($values); // Returns a list of values: 'Laptop', 98000, 15.

// Search for keys associated with a specific value.
$scores = ['English' => 80, 'Math' => 95, 'Science' => 80, 'History' => 90];
$subjects = array_keys($scores, 80);
print_r($subjects); // Returns subjects with a score of 80: 'English', 'Science'.

// Extract a specific column from a multidimensional array.
$users = [
    ['id' => 1, 'name' => 'Alice', 'age' => 25],
    ['id' => 2, 'name' => 'Bob', 'age' => 30],
    ['id' => 3, 'name' => 'Carol', 'age' => 20],
];
$names = array_column($users, 'name');
print_r($names); // Returns an array: 'Alice', 'Bob', 'Carol'.

// Specify an index key using the third argument.
$by_id = array_column($users, 'name', 'id');
print_r($by_id); // Returns an associative array keyed by ID.

// Swap keys and values.
$colors = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
$flipped = array_flip($colors);
print_r($flipped); // Values become keys, and keys become values.

Details

array_keys() not only retrieves a list of keys from an associative array, but also lets you search for keys that have a specific value by passing that value as the second argument. Passing true as the third argument enables strict comparison, including type checking.

array_values() is useful when you want to convert an associative array into a standard numerically indexed array. It is also commonly used to re-index arrays with non-consecutive keys — for example, after calling array_unique() or array_filter().

array_column() is extremely useful for extracting a specific column from multidimensional arrays, such as database result sets or API responses. By specifying a third argument, you can create an associative array keyed by the values of that column.

For merging or splitting arrays, see array_merge() / array_combine(). For searching within arrays, see in_array() / array_search().

If you find any errors or copyright issues, please .