Skip to main content

in_array PHP Explained: How to Check Values in Arrays Like a Pro

in_array PHP – Complete Guide with Examples and Outputs

in_array PHP – Complete Guide with Examples and Outputs

The in_array PHP function is used to check if a given value exists in an array. It is one of the most commonly used array functions in PHP and works with both indexed and associative arrays.

Syntax of in_array PHP

in_array(mixed $needle, array $haystack, bool $strict = false): bool
  • $needle – The value to search for.
  • $haystack – The array to search in.
  • $strict – If true, the function also checks the type of the value.

Example 1: Basic Indexed Array

<?php
$fruits = ["Apple", "Banana", "Mango"];
if (in_array("Banana", $fruits)) {
    echo "Banana is in the list.";
} else {
    echo "Banana is not in the list.";
}
?>

Output: Banana is in the list.

Example 2: Associative Array

<?php
$person = [
    "name" => "John",
    "age" => 25,
    "city" => "New York"
];
if (in_array("John", $person)) {
    echo "John is found in the array.";
} else {
    echo "John is not found.";
}
?>

Output: John is found in the array.

Example 3: Strict Comparison

<?php
$numbers = [1, "1", 2];
if (in_array(1, $numbers, true)) {
    echo "Strict match for 1 found.";
} else {
    echo "No strict match found.";
}
?>

Output: Strict match for 1 found.

Example 4: Value Not Found

<?php
$colors = ["Red", "Blue", "Green"];
if (in_array("Yellow", $colors)) {
    echo "Yellow is in the array.";
} else {
    echo "Yellow is not in the array.";
}
?>

Output: Yellow is not in the array.

Example 5: Checking Boolean Values

<?php
$values = [true, false, 0, 1];
if (in_array(true, $values, true)) {
    echo "True is found with strict checking.";
} else {
    echo "True is not found.";
}
?>

Output: True is found with strict checking.

Conclusion

The in_array PHP function is simple yet powerful for validating values within arrays. It’s useful for form validations, condition checks, and data filtering. Remember to use the $strict parameter when you want to ensure both value and type match.

Comments

Popular posts from this blog

PHP Array Push Multiple Values: A Complete Guide | StackCodee

PHP Array Push Multiple Values: A Complete Guide | StackCodee PHP Array Push Multiple Values: A Complete Guide 📅 November 8, 2025 ⏱️ 8 min read 🏷️ PHP, Arrays, Programming Welcome to StackCodee, your go-to resource for practical programming knowledge. In this comprehensive guide, we'll explore the various methods to push multiple values to a PHP array efficiently. Whether you're a beginner or an experienced developer, understanding these techniques will enhance your array manipulation skills in PHP. Understanding PHP Arrays PHP arrays are incredibly versatile data structures that can hold multiple values of different types. They can be indexed numerically or associatively with key-value pairs, and they can even function as lists, stacks, or queues. ...

PHPMyAdmin Localhost:8080 Setup Guide

PHPMyAdmin Localhost:8080 Setup Guide PHPMyAdmin Localhost:8080 Setup Guide Learn how to install, configure, and troubleshoot PHPMyAdmin running on localhost port 8080 with detailed examples and solutions to common problems. ⏱️ 10 min read 🏷️ PHPMyAdmin, MySQL, Localhost, Web Development Installation Steps 1 Install PHPMyAdmin Download and install PHPMyAdmin on your local server environment (XAMPP, WAMP, MAMP, or manual setup). # For Ubuntu/Debian systems sudo apt-get install phpmyadmin # For CentOS/RHEL systems sudo yum install phpmyadmin # Or download direc...

PHP array_merge_recursive() Function — Explained

PHP array_merge_recursive() — Preserve Numeric Keys 📖 PHP array_merge_recursive() Function — Explained The array_merge_recursive() function in PHP is used to merge two or more arrays recursively. It helps you combine values under the same keys without overwriting any data. Unlike array_merge() , which replaces duplicate keys, this function keeps both values by converting them into arrays. 🔑 Keyword Focus: php array_merge_recursive preserve numeric keys If you're trying to preserve numeric keys while merging arrays in PHP, it's important to know that array_merge_recursive() will reindex numeric keys by default. Let’s see what that means. 📌 Syntax array_merge_recursive(array $array1, array $array2, ...): array Parameters: $array1, $array2, ... – Arrays you want to merge Returns: New merged array. String keys: merged into arrays. Numeric keys: re...