Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions PHP Internal Functions PHP Array Functions

Quiz question about array_keys

One of the questions is : "What does the optional argument(s) for array_keys do for us?" Looking at the PHP docs I understood $search_value argument contains value that we lookup within values (not keys), and this function returns corresponding keys (array of).

So which answer is right:

A Allows you to search for a value in the keys and provide the option to make your search identical or just equal in match

B Allows you to search for a value in the values and provide the option to make your search identical or just equal in match

I suppose it's an error marking B as wrong answer.

I took the quiz twice and selected A) A Allows you to search for a value in the keys and provide the option to make your search identical or just equal in match

...and it told me it was wrong. B is the correct answer: Allows you to search for a value in the values and provide the option to make your search identical or just equal in match.

From the docs:

If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

3 Answers

I think A is the answer. The array_keys() function returns the keys from an array. You can optionally get a subset of the keys by adding arguments. The optional strict would force the key to be identical to the other optional argument. Since it returns values for the key and not values for the values, the answer is A.

See this section of the PHP manual.

Ted, I've read docs during video lessons, and so being even more positive about my opinion. However, since English is not my native language, I leave some reserve for not understanding everything right. And docs aren't perfect as well.

Hence, the return type is quite clear (array of keys). Strict option is clear also (identical). The $search_value option was clear to me until this quiz, though.

I think in array_keys() we can't lookup for keys within keys and return - keys. We search for specific values within values and return corresponding keys. So B is correct, to my opinion.

Here is the example from docs:

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

returns

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)

As I understand it, the array_keys function returns a numerically-indexed array of the keys from the array you are searching. The value argument allows you to specify a value and return only the keys which have that matching value. In addition, the strict argument would force the value to be of the same data type, as well. So, B is the correct answer.

I just took the quiz and the correct answer is the one I thought. Boris, your answer confirms my explanation. The returned array gives the key value of the locations of blue. In your example, blue appears in spots 0, 3, and 4 of $array. So array_keys() looked for the key word 'blue' and returned the key values where it occurs in the array.

Hm, perhaps I didn't understand the question. Keys... values... values of keys... keys of values and so on, in order to confuse audience : ).

But I think it's simple:

Any array is a map of key-value pairs.

Function array_keys() returns array of keys.

One optional argument refers to specific value and returns array of just those keys that correspond to specified value. So where it searched for word 'blue'? - in values, not in keys. Keys are returned but not searched upon 'blue'. Values are searched.

Anyway, I don't think this is much important. The function works as it's expected no matter how we name it.

I think you understand how it really works, which is the important part. You search an array for something and the array_keys() returns the key that will give you the term you searched for.