How to check if a value exists in an array in PHP. In this tutorial, I am going to discuss PHP inbuilt and custom method to check if value exists in array.
PHP provides couple of in-built methods to check whether a value exists in an array or not. Methods such as in_array(), array_search() etc.
Let’s discuss, how we can use these methods as well as we write our own custom method to check if a value exists in an array.
PHP in_array() method to Check If a Value Exists in an Array in PHP
PHP in_array() method check if a value exists in an array, It returns true if a value exists otherwise false.
Syntax –
1 2 3 4 5 |
in_array(value, array, type) value : Value to be searched . Required field. array : Array to search. Required field. type : Check the type. Optional field. |
1. Let’s first take the case of a simple array.
1 2 3 4 5 6 7 8 9 10 11 |
// array $mobile = array("Nokia", "Micromax", "Iphone", "Lava"); /* Checking if Lava exists in an array. */ if (in_array("Lava", $mobile)) { echo "Value exists"; } else { echo "Value doesn't exists"; } // Output - Value exists |
How to check if a value exists in an array using jquery/javascript
Top five most used PHP array functions
PHP : Check if value exists in Associative array
In above example, we have used in_array() method for a simple array. Let’s check in_array() method for an associative array.
1 2 3 4 5 6 7 8 9 10 |
// Associative array. $mobile = array(0=>"Nokia", 1=>"Micromax", 2=>"Iphone", 3=>"Lava"); if (in_array("Lava", $mobile)) { echo "Value exists"; } else { echo "Value doesn't exists"; } // Output - Value exists |
How to return multiple values from a function in PHP
PHP array_search() method to check if a value exists in an array in PHP
PHP array_search() method search an array for given value and return the corresponding key if a value exists in an array. If a value doesn’t exist in an array then it returns NULL.
How to sort string using PHP code
Syntax –
1 |
array_search(value, array, strict); |
if strict is set to true which is false by default, then it searches for an identical element (strict type checking). In PHP, Strict type comparison is done through triple equal to (===).
Difference between double and triple equals to in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Associative array $mobile = array(0 => "Nokia", 1 => "Micromax", 2 => "Iphone", 3=>"Lava"); echo array_search("Lava", $mobile); // Print 3 if (array_search("Lava", $mobile)) { echo "Value exists"; } else { echo "Value doesn't exists"; } // output - Value exists |
PHP Function to check if a value exists in an array
In above example, We have discussed PHP inbuilt functions to check if a value exists in an array. Let’s create our own custom function which takes two arguments ( array and the value to be searched). It compares the value to each element of an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* array */ $mobile = array(0=>"Nokia", 1=>"Micromax", 2=>"Iphone", 3=>"Lava"); inArray($mobile,'Lava'); // Output - value exists /* Take two parameter array and value to be searched. */ function inArray($array, $value){ /* Initialize index -1 initially. */ $index = -1; foreach($array as $val){ /* If value is found, set index to 1. */ if($val == $value){ $index = 1; } } if($index == -1){ echo "value does not exists"; } else { echo "value exists"; } } |
Conclusion
I have explained all the methods which I used to check whether a value exists in an array or not. If you know any other method then you can let us know through your comments.