Challenge - 5 Problems
Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using array_search?
Consider the following PHP code snippet. What will it output?
PHP
<?php $array = ['apple', 'banana', 'cherry']; $pos = array_search('banana', $array); echo $pos; ?>
Attempts:
2 left
💡 Hint
Remember that array_search returns the key of the found element.
✗ Incorrect
The array has 'banana' at index 1, so array_search returns 1.
❓ Predict Output
intermediate2:00remaining
What does array_search return when the element is not found?
What will this PHP code output?
PHP
<?php $array = ['red', 'green', 'blue']; $result = array_search('yellow', $array); var_dump($result); ?>
Attempts:
2 left
💡 Hint
Check what array_search returns if the value is not found.
✗ Incorrect
array_search returns false if the searched value is not found in the array.
🔧 Debug
advanced2:00remaining
Why does this array_search code produce unexpected output?
Look at this PHP code. It tries to find the key of the value 0 in the array. What is the output and why?
PHP
<?php $array = [0 => 'zero', 1 => 'one', 2 => 'two']; $key = array_search(0, $array); echo $key === false ? 'Not found' : $key; ?>
Attempts:
2 left
💡 Hint
Check the types of values in the array and what is searched.
✗ Incorrect
The array contains strings, but the search is for integer 0, which is not found, so array_search returns false.
❓ Predict Output
advanced2:00remaining
What is the output when using strict comparison in array_search?
What will this PHP code output?
PHP
<?php $array = ['1', '2', '3']; $key = array_search(1, $array, true); echo $key === false ? 'Not found' : $key; ?>
Attempts:
2 left
💡 Hint
Strict comparison checks both value and type.
✗ Incorrect
With strict=true, integer 1 is not equal to string '1', so array_search returns false.
🧠 Conceptual
expert2:00remaining
How many elements are in the array after this code?
What is the number of elements in the array after this PHP code runs?
PHP
<?php $array = ['a' => 1, 'b' => 2, 'c' => 3]; $key = array_search(2, $array); if ($key !== false) { unset($array[$key]); } echo count($array); ?>
Attempts:
2 left
💡 Hint
Removing one element reduces the count by one.
✗ Incorrect
array_search finds key 'b', unset removes it, so array has 2 elements left.