Recall & Review
beginner
What function in PHP extracts all the keys from an array?
The
array_keys() function extracts all the keys from an array and returns them as a new array.Click to reveal answer
beginner
How do you get all the values from an array in PHP?
Use the
array_values() function to get all the values from an array as a new indexed array.Click to reveal answer
beginner
Given
$arr = ['a' => 1, 'b' => 2];, what does array_keys($arr) return?It returns
['a', 'b'], which are the keys of the array.Click to reveal answer
beginner
What will
array_values(['x' => 10, 'y' => 20]) return?It returns
[10, 20], the values of the array re-indexed starting from 0.Click to reveal answer
intermediate
Can
array_keys() be used to find keys for a specific value?Yes, by passing the value as the second argument,
array_keys($array, $value) returns all keys that have that value.Click to reveal answer
Which PHP function returns all keys from an array?
✗ Incorrect
array_keys() returns all keys from an array.What does
array_values() return?✗ Incorrect
array_values() returns all values of an array as a new indexed array.How can you find all keys for a specific value in an array?
✗ Incorrect
Passing the value as the second argument to
array_keys() returns all keys matching that value.What will
array_keys(['x' => 5, 'y' => 10]) return?✗ Incorrect
array_keys() returns the keys, which are 'x' and 'y'.If you want only the values of an array without keys, which function do you use?
✗ Incorrect
array_values() returns the values without keys.Explain how to extract keys and values from an array in PHP.
Think about the two main functions for keys and values.
You got /4 concepts.
How can you find all keys associated with a specific value in a PHP array?
Use array_keys with a value parameter.
You got /3 concepts.