0
0
PHPprogramming~10 mins

Array search functions in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the value 5 exists in the array.

PHP
<?php
$numbers = [1, 3, 5, 7];
if (in_array([1], $numbers)) {
    echo "Found!";
} else {
    echo "Not found.";
}
?>
Drag options to blanks, or click blank then click option'
A0
B7
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not present in the array.
Confusing the array variable with the value.
2fill in blank
medium

Complete the code to find the index of the value 3 in the array.

PHP
<?php
$letters = ['a', 'b', 'c', 'd'];
$index = array_search([1], $letters);
echo $index;
Drag options to blanks, or click blank then click option'
A'b'
B'c'
C'a'
D'd'
Attempts:
3 left
💡 Hint
Common Mistakes
Searching for a value not in the array.
Confusing the value with the index.
3fill in blank
hard

Fix the error in the code to correctly check if the key 'color' exists in the array.

PHP
<?php
$info = ['color' => 'red', 'size' => 'large'];
if (array_key_exists([1], $info)) {
    echo "Key found.";
} else {
    echo "Key not found.";
}
?>
Drag options to blanks, or click blank then click option'
A'red'
B'color'
C'size'
D'large'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a value instead of a key.
Using the wrong key name.
4fill in blank
hard

Fill both blanks to create an array of keys from the array and check if 'age' is one of them.

PHP
<?php
$user = ['name' => 'Alice', 'age' => 25];
$keys = [1]($user);
if (in_array([2], $keys)) {
    echo "Age key exists.";
} else {
    echo "Age key missing.";
}
?>
Drag options to blanks, or click blank then click option'
Aarray_keys
B'age'
C'name'
Darray_values
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_values instead of array_keys.
Checking for a value instead of a key.
5fill in blank
hard

Fill all three blanks to find the index of 'blue' in the colors array and print a message if found.

PHP
<?php
$colors = ['red', 'green', 'blue', 'yellow'];
$pos = array_search([1], $colors);
if ($pos !== [2]) {
    echo "Found color at index [3].";
} else {
    echo "Color not found.";
}
?>
Drag options to blanks, or click blank then click option'
A'blue'
Bfalse
C$pos
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with null instead of false.
Printing the wrong variable for the index.