How to check if a value exists in PHP array
In this post we will give you information about How to check if a value exists in PHP array. Hear we will give you detail about How to check if a value exists in PHP arrayAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to check if a given value exists or not in an array in PHP.
Using in_array() function
To check if a value is exists in array or not, we can use the built-in in_array() function in PHP.
The in_array() function takes two arguments, the first argument is search value, the second argument is array itself and it returns true if a value is found in array; otherwise it returns false.
Here is an example:
$cars = array("Audi", "BMW", "Benz");;if (in_array("BMW", $cars)) { echo "value is found";}else{ echo "value is not found";}
Output:
"value is found"
False case example:
$cars = array("Audi", "BMW", "Benz");;if (in_array("ford", $cars)) { echo "value is found";}else{ echo "value is not found";}
Output:
"value is not found"
In the above example, we have passed the search value “ford” which is not available in $cars arrayso it returns output value is not found.
Hope this code and post will helped you for implement How to check if a value exists in PHP array. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs