Sometimes you want to get just the keys or just the values from an array to use them separately.
Array key and value extraction in PHP
<?php // Extract keys from an array $keys = array_keys($array); // Extract values from an array $values = array_values($array); ?>
array_keys() returns a new array with all the keys from the original array.
array_values() returns a new array with all the values from the original array, re-indexed starting at 0.
<?php // Example with associative array $person = ['name' => 'Alice', 'age' => 30]; $keys = array_keys($person); $values = array_values($person); print_r($keys); print_r($values); ?>
<?php // Example with empty array $empty = []; $keys = array_keys($empty); $values = array_values($empty); print_r($keys); print_r($values); ?>
<?php // Example with indexed array $numbers = [10, 20, 30]; $keys = array_keys($numbers); $values = array_values($numbers); print_r($keys); print_r($values); ?>
<?php // Example with single element array $single = ['only' => 'one']; $keys = array_keys($single); $values = array_values($single); print_r($keys); print_r($values); ?>
This program creates an array of fruits with their colors, then extracts and prints the keys (fruit names) and values (colors) separately.
<?php // Create an associative array $fruits = [ 'apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple' ]; // Print original array echo "Original array:\n"; print_r($fruits); // Extract keys $fruit_names = array_keys($fruits); // Extract values $fruit_colors = array_values($fruits); // Print keys echo "\nExtracted keys (fruit names):\n"; print_r($fruit_names); // Print values echo "\nExtracted values (fruit colors):\n"; print_r($fruit_colors); ?>
Time complexity: Both array_keys() and array_values() run in O(n) time, where n is the number of elements.
Space complexity: They create new arrays, so space used is O(n).
A common mistake is expecting array_values() to keep original keys; it always re-indexes starting at 0.
Use array_keys() when you need to work with keys only, and array_values() when you want just the values without keys.
You can get all keys from an array using array_keys().
You can get all values from an array using array_values().
These functions help separate keys and values for easier processing.
Practice
Solution
Step 1: Understand the function purpose
array_keys()returns all keys from an array.Step 2: Compare with other functions
array_values()returns values, not keys; others do different tasks.Final Answer:
array_keys() -> Option CQuick Check:
Keys extraction = array_keys() [OK]
- Confusing array_keys() with array_values()
- Using array_map() to get keys
- Trying to get keys with array_filter()
$arr in PHP?Solution
Step 1: Identify the correct function name
The function to get values isarray_values().Step 2: Check syntax correctness
Correct syntax isarray_values($arr);with parentheses and variable inside.Final Answer:
array_values($arr); -> Option AQuick Check:
Values extraction = array_values() [OK]
- Using array_keys() to get values
- Using non-existent functions like values_array()
- Missing parentheses in function call
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; print_r(array_keys($arr));
Solution
Step 1: Understand array_keys() output
It returns an indexed array of all keys from the input array.Step 2: Analyze given array keys
Keys are strings 'a', 'b', 'c', so output is an indexed array with these strings.Final Answer:
Array ( [0] => a [1] => b [2] => c ) -> Option DQuick Check:
array_keys(['a'=>1,...]) = ['a','b','c'] [OK]
- Confusing keys with values in output
- Expecting associative array output
- Including quotes in output keys
$arr = ['x' => 10, 'y' => 20]; $keys = array_values($arr); print_r($keys);
Solution
Step 1: Understand the goal
The code tries to get keys but usesarray_values()which returns values.Step 2: Correct function usage
To get keys, usearray_keys()instead ofarray_values().Final Answer:
array_values() should be array_keys() to get keys -> Option AQuick Check:
Keys need array_keys(), not array_values() [OK]
- Mixing up array_keys() and array_values()
- Assuming print_r() can't print arrays
- Ignoring correct variable definitions
$data = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => 0];, which PHP code correctly extracts only the keys with non-zero values?Solution
Step 1: Filter array to keep non-zero values
array_filter($data)removes entries with zero values.Step 2: Extract keys from filtered array
array_keys()gets keys from the filtered array, giving only keys with non-zero values.Final Answer:
$keys = array_keys(array_filter($data)); -> Option BQuick Check:
Filter then keys = array_keys(array_filter()) [OK]
- Using array_values() instead of array_keys() after filtering
- Not filtering before extracting keys
- Filtering keys instead of values
Ready to prove it?
Pass the test and this pattern moves unknown
