PHP array_keys and PHP array_values are two important functions. The array_keys function returns the keys from the input array, and the array_values return the values. The array_keys take some additional parameters for returning keys corresponding to a value that matches the search_value argument.
Both of these functions return an array. So, this article reviews these functions and helps the readers understand the difference between array_keys and array_values. To have heads on, here are the articles explaining the PHP arrays_keys and PHP array_values in depth.
- PHP array_keys: how to use in PHP with examples
- PHP array_values: how to use in PHP with examples
What is the difference between array_keys and array_values
This section reviews the array_keys PHP and the array_values PHP and includes examples to clarify the difference between array_keys and array_values.
what is the array_keys function
Here’s an overview of the array_keys in PHP.
Function Signature
array_keys(array $array, mixed $search_value, bool $strict = false): array
Description
The array_keys function takes an array argument and some optional arguments. These arguments are search_value and strict. The search_value argument finds a value and returns its corresponding key or keys if the value appears more than once. The strict argument indicates whether the function should match the value and data type or just the value.

PHP array_keys Code Examples
Here’s how the array_keys function works.
<?php
$numbers = ["One"=>1, "Two"=>2, "Three"=>3, "Four"=>4, "Five"=>5];
print_r(array_keys($numbers));
/*
OUTPUT
Array
(
[0] => One
[1] => Two
[2] => Three
[3] => Four
[4] => Five
)
*/
Easy! It takes the array and returns the keys, numerically indexing them.
Next, let’s see the function with the search_value argument.
<?php
$developers = [
"Anna" => "Javascript",
"Edward" => "Java",
"Rey" => "Python",
"Sanjay" => "PHP",
"Ali" => "C++",
"Sarah" => "C#",
"Bob" => "Android",
"Tyson" => "PHP",
"Greg" => "PHP"
];
print_r(array_keys($developers, "PHP"));
/*
OUTPUT
Array
(
[0] => Sanjay
[1] => Tyson
[2] => Greg
)
*/
?>
Voila! It takes “PHP” as the search_value and returns the key that has the PHP value. These keys are developers’ names: Sanjay, Tyson, and Greg.
PHP array_keys with the array_search
PHP array_keys can be used with the array_search function to find if a key exists in an array. The following example shows how that works.
<?php
$name_age = [
"David" => 30,
"Martha" => 43,
"Tyson" => 18,
"Sarah" => 22,
"Ralph" => 19
];
print_r(array_search("David", array_keys($name_age))); //0
print_r(array_search("Brian", array_keys($name_age))); //false
print_r(array_search("Ralph", array_keys($name_age))); //4
?>
So, the array_keys function returns a numerically indexed array of keys, and the array_search returns the numeric index if it finds the key. These functions are helpful to confirm if a key exists in an array.
What is the array_values function in PHP
Here’s an overview of the array_values PHP.
Function Signature
array_values(array $array): array
Description
The array_values in PHP take an array argument and returns the value, numerically indexing them. The function doesn’t preserve the keys because that would defeat the purpose of this function.

PHP array_values Code Example
So, let’s see an example of the PHP array_values.
<?php
$items = ["Fruit"=>"Apple","Vegetable"=>"Carrot","Dairy"=>"Cheese"];
print_r(array_values($items));
/*
OUTPUT
Array
(
[0] => Apple
[1] => Carrot
[2] => Cheese
)
*/
?>
So, the array_values returns the value, numerically indexing them.
Conclusion
So, the article clarifies the difference between array_keys and array_values by reviewing these functions using appropriate examples. The difference between these function is that the array_keys extract the keys out from an array while the array_values does the same for values.
Apart from that, the array_keys function has optional parameters for searching a value and returning the corresponding keys only. So, that’s all about the difference between array_keys and array_values. Hope that you have learned something new today. Stay tuned for more exciting and informative articles and tutorials related to PHP.
Want to explore more useful PHP tutorials?
We have many fun articles related to PHP. You can explore these to learn more about PHP.
How to merge 2 arrays with the same keys in PHP