Values with 'in_array'
Part of the Logic & Flow section of Coddy's PHP journey — lesson 17 of 68.
When you need to check whether a specific value exists within an array, PHP's in_array() function provides the perfect solution. This function searches through an array and returns a boolean value—true if the value is found, or false if it's not.
Here's the basic syntax:
<?php
$existingUsers = ["alice", "bob", "charlie"];
$newUsername = "bob";
if (in_array($newUsername, $existingUsers)) {
echo "Username already taken!";
} else {
echo "Username available!";
}
// Outputs: Username already taken!
?>The in_array() function takes two main parameters: the value you're searching for and the array to search in. Since it returns a boolean, it works perfectly with conditional statements like if and while loops.
This function is particularly useful for validation tasks, such as checking if a user's input matches one of several allowed options, verifying if an item already exists in a shopping cart, or ensuring that duplicate entries aren't added to a list.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive two inputs: an array of available colors in JSON format and a color name to search for. Read both inputs, convert the JSON string to an array, use in_array() to check if the color exists in the array, and print "Color available" if found or "Color not available" if not found.
Input format: Two lines - first line contains a JSON array (example: ["red","blue","green","yellow"]), second line contains the color name to search for
Expected output: Either "Color available" or "Color not available" based on whether the color exists in the array
Cheat sheet
The in_array() function searches for a value within an array and returns true if found, false otherwise.
Syntax:
in_array($value, $array)Example:
<?php
$existingUsers = ["alice", "bob", "charlie"];
$newUsername = "bob";
if (in_array($newUsername, $existingUsers)) {
echo "Username already taken!";
} else {
echo "Username available!";
}
// Outputs: Username already taken!
?>The function works perfectly with conditional statements and is commonly used for validation tasks like checking user permissions, preventing duplicates, or verifying allowed options.
Try it yourself
<?php
// Read the JSON array of colors
$jsonInput = fgets(STDIN);
$colors = (array)json_decode($jsonInput, true);
// Read the color to search for
$searchColor = trim(fgets(STDIN));
// TODO: Write your code below to check if the color exists in the array
// Output the result
echo $result;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting