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.
Difference between array_keys and array_values: array_keys review
Here’s an overview of the array_keys in PHP.
array_keys(array $array, mixed $search_value, bool $strict = false): array
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.
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 (  => One  => Two  => Three  => Four  => Five ) */
Easy! It takes the array and returns the keys, numerically indexing them.
Next, let’s see the function with the search_value argument.
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.
Difference between array_keys and array_values: array_values review
Here’s an overview of the array_values PHP.
array_values(array $array): array
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.
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 (  => Apple  => Carrot  => Cheese ) */ ?>
So, the array_values returns the value, numerically indexing them.
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.