0
0
PHPprogramming~20 mins

Array search functions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
?>
A0
B1
C2
Dfalse
Attempts:
2 left
💡 Hint
Remember that array_search returns the key of the found element.
Predict Output
intermediate
2: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);
?>
Abool(false)
Bint(0)
CNULL
Dstring('')
Attempts:
2 left
💡 Hint
Check what array_search returns if the value is not found.
🔧 Debug
advanced
2: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;
?>
A0
B2
CNot found
D1
Attempts:
2 left
💡 Hint
Check the types of values in the array and what is searched.
Predict Output
advanced
2: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;
?>
ANot found
B1
C0
D2
Attempts:
2 left
💡 Hint
Strict comparison checks both value and type.
🧠 Conceptual
expert
2: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);
?>
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Removing one element reduces the count by one.