How to check if a value is present in an array in PHP? (PHP in array)

In some cases, we may want to check whether a particular value is present in an array. This can be achieved using predefined PHP function in_array


PHP in Array

PHP has a predefined function in_array function and it checks whether a particular value is present in an array. 

PHP in_array function takes two parameter. One parameter is the value which we want to check whether present in the array and the second parameter is array itself.

Note: in_array function always returns a boolean value only. (Either true or false)

Example

$fruitNames is an array variable which contains fruit names. Lets check this array variable contains "banana" or not. 
<?php
$fruitNames=array("apple","banana","orange","guava","jackfruit");
if(in_array("banana",$fruitNames)){
echo "Banana is present in the array";
}
?>
Output

Banana is present in the array

How to check if a value is present in an array in PHP? (PHP in array) How to check if a value is present in an array in PHP? (PHP in array) Reviewed by Pixel Stream on December 10, 2019 Rating: 5

No comments:

Powered by Blogger.