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_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...