Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You 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;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow