How to Check If a Value Exists in An Array in PHP

How to Check If a Value Exists in An Array in PHP

How do I check if a specific value exists in an array in PHP. Write a sample PHP program to check if a value exists in an array. Using PHP in_array() function Use PHP in_array() function to check whether a specific value exists in an array or not. Here is an sample PHP program, […]

How do I check if a specific value exists in an array in PHP. Write a sample PHP program to check if a value exists in an array.

Using PHP in_array() function

Use PHP in_array() function to check whether a specific value exists in an array or not.

Here is an sample PHP program, initialized an array with few element. Then check if an element is available in the defined array.

Example


<?php
$colors = array("Yellow", "Green", "Blue", "Pink", "Black");

if(in_array("Green", $colors)){
    echo "Green element found in colors array";
}
?>

Output

Green element found in colors array

General Articles array PHP

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Article Series: PHP 7.x

PHP 7.x brings several improvements and new features that touch all aspects of the language, including better support for object oriented programming, extensions to classes and interfaces, improvements to the type system, error handling, and more. In this series of articles, we discuss new features across the various PHP 7.x versions.

PHP Arrays

An array could be said to be a collection of data. It could be an empty collection like an empty box, or with just one item, like a car with just one person in it — it could take more you know, or with as many items as you want.

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

PHP Array Count Example | PHP count() Function Tutorial

PHP Array Count Example | PHP count() Function Tutorial. PHP Array count() method returns the number of elements in an array and multidimensional array.

PHP Array Sum Example | PHP array_sum() Function Tutorial

PHP Array Sum Example | PHP array_sum() Function Tutorial. The array_sum() function returns the sum of all the values in the array.