Bird
Raised Fist0
PHPprogramming~10 mins

Array search functions in PHP - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. Which PHP function returns the key of a value in an array if found, or false if not found?
easy
A. in_array
B. array_search
C. array_key_exists
D. array_keys

Solution

  1. Step 1: Understand function purpose

    array_search looks for a value and returns its key if found.
  2. Step 2: Compare with other functions

    in_array returns true/false, not key; array_key_exists checks keys; array_keys returns all keys.
  3. Final Answer:

    array_search -> Option B
  4. Quick Check:

    Key returned by array_search = array_search [OK]
Hint: array_search returns key, in_array returns true/false [OK]
Common Mistakes:
  • Confusing array_search with in_array
  • Thinking array_key_exists returns value
  • Assuming array_keys searches values
2. Which of the following is the correct syntax to check if the value 5 exists in array $arr using in_array?
easy
A. in_array(5, $arr);
B. in_array($arr, 5);
C. in_array($arr => 5);
D. in_array(5 => $arr);

Solution

  1. Step 1: Recall in_array syntax

    The correct syntax is in_array(value, array).
  2. Step 2: Match syntax with options

    in_array(5, $arr); matches in_array(5, $arr); which is correct.
  3. Final Answer:

    in_array(5, $arr); -> Option A
  4. Quick Check:

    in_array(value, array) = in_array(5, $arr) [OK]
Hint: Remember in_array(value, array) order [OK]
Common Mistakes:
  • Swapping parameters order
  • Using arrow syntax instead of comma
  • Passing array as first argument
3. What is the output of the following PHP code?
$arr = ['apple', 'banana', 'cherry'];
$key = array_search('banana', $arr);
echo $key;
medium
A. 0
B. banana
C. false
D. 1

Solution

  1. Step 1: Understand array_search behavior

    It returns the key of the searched value if found.
  2. Step 2: Check array keys and searched value

    Array keys are 0=>'apple', 1=>'banana', 2=>'cherry'. Searching 'banana' returns key 1.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    array_search('banana', $arr) = 1 [OK]
Hint: array_search returns index/key of found value [OK]
Common Mistakes:
  • Expecting value instead of key
  • Confusing false with 0
  • Assuming it returns boolean
4. Identify the error in the following PHP code snippet:
$arr = ['x' => 10, 'y' => 20];
if (array_search(30, $arr)) {
    echo 'Found';
} else {
    echo 'Not Found';
}
medium
A. array_search returns false but condition treats 0 as false
B. array_search returns key but 30 is not in array, so condition fails correctly
C. array_search should be replaced with in_array for this check
D. No error, code works as expected

Solution

  1. Step 1: Analyze array_search return and condition

    array_search returns key or false. If key is 0, condition fails because 0 is falsey.
  2. Step 2: Check searched value presence

    30 is not in array, so array_search returns false, condition is false, prints 'Not Found'. But if value was at key 0, condition would fail incorrectly.
  3. Step 3: Suggest correct function for boolean check

    Use in_array which returns true/false directly for presence check.
  4. Final Answer:

    array_search should be replaced with in_array for this check -> Option C
  5. Quick Check:

    Use in_array for boolean presence check [OK]
Hint: Use in_array for true/false presence checks [OK]
Common Mistakes:
  • Using array_search in if without strict check
  • Ignoring 0 key treated as false
  • Not using === to check false
5. Given the array $data = ['a' => 0, 'b' => false, 'c' => null, 'd' => '0'];, which function call correctly checks if the value 0 exists in the array considering type?
hard
A. in_array(0, $data, true);
B. array_search(0, $data);
C. in_array(0, $data);
D. array_search('0', $data, true);

Solution

  1. Step 1: Understand strict checking

    Using third parameter true enforces type checking in in_array and array_search.
  2. Step 2: Analyze array values and types

    Values are 0 (int), false (bool), null, and '0' (string). Only strict check distinguishes 0 (int) from others.
  3. Step 3: Choose correct function and parameters

    in_array(0, $data, true) returns true only if exact int 0 found. array_search without strict may confuse types.
  4. Final Answer:

    in_array(0, $data, true); -> Option A
  5. Quick Check:

    Use in_array with strict=true for exact type match [OK]
Hint: Use in_array with strict=true to check exact value and type [OK]
Common Mistakes:
  • Ignoring strict parameter causing false positives
  • Using array_search without strict for type-sensitive check
  • Confusing '0' string with 0 integer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard