How to Check If a Value Exists in an Array in PHP?
Hi Guys,
In this example, I will show you php array check value exists. I’m going to show you about how to check value exists in array in php. I would like to show you how to check key value exists in array in php. It's a simple example of how to check value exist in array or not in php. you will do the following things for php laravel check array value exists or not.
we will use in_array() function and isset() function to check value exists or not in php array. so, let's see the simple code of how to check value exists in array in php.
Example 1:
index.php
<?php
$array = ['id' => 1, 'name' => 'Hardik', 'email' => 'hardik@gmail.com'];
if (in_array('Hardik', $array)) {
echo('Value is exists.');
} else {
echo('Value is not exists.');
}
?>
Output:
Value is exists.
Example 2:
index.php
<?php
$array = ['id' => 1, 'name' => 'Hardik', 'email' => 'hardik@gmail.com'];
if (isset($array['name']) && $array['name'] == 'Hardik') {
echo('Value is exists.');
} else {
echo('Value is not exists.');
}
?>
Output:
Value is exists.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Check Array Empty or Not in PHP?
- PHP array_merge() Function Example
- How to Merge Two Arrays with Unique Values in PHP?
- How to Merge Two Array in PHP?
- How to Remove String Keys in PHP Array?
- How to Remove Numeric Keys in PHP Array?
- How to Remove String Values in PHP Array?
- How to Remove Numeric Values in PHP Array?
- How to Get First 10 Elements of Array in PHP?
- How to Get First 3 Elements of Array in PHP?
- How to Get Last 10 Elements of Array in PHP?
- How to Get Last 2 Elements of Array in PHP?
- How to Convert String into Array in PHP?
- How to Remove Last Element from Array in PHP?