Complete the code to check if the value 5 exists in the array.
<?php $numbers = [1, 3, 5, 7]; if (in_array([1], $numbers)) { echo "Found!"; } else { echo "Not found."; } ?>
The in_array function checks if a value exists in an array. Here, we want to check for 5.
Complete the code to find the index of the value 3 in the array.
<?php $letters = ['a', 'b', 'c', 'd']; $index = array_search([1], $letters); echo $index;
The array_search function returns the index of the searched value. The value 'b' is at index 1.
Fix the error in the code to correctly check if the key 'color' exists in the array.
<?php $info = ['color' => 'red', 'size' => 'large']; if (array_key_exists([1], $info)) { echo "Key found."; } else { echo "Key not found."; } ?>
The array_key_exists function checks if a key exists in an array. The key to check is 'color'.
Fill both blanks to create an array of keys from the array and check if 'age' is one of them.
<?php $user = ['name' => 'Alice', 'age' => 25]; $keys = [1]($user); if (in_array([2], $keys)) { echo "Age key exists."; } else { echo "Age key missing."; } ?>
array_keys gets all keys from the array. Then in_array checks if 'age' is among those keys.
Fill all three blanks to find the index of 'blue' in the colors array and print a message if found.
<?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."; } ?>
array_search returns the index or false if not found. We check if the result is not false and then print the index.