PHP in_array Function | How to use in_array in PHP
Summary: In this article, you learn PHP in_array function and how to use it with example. Let’s understand this article in details with various example.
PHP in_array Definition and Usage
The PHP in_array() function is an inbuilt function. The in_array() function is used to searches an array for a specific value or used to check whether a given value exists in an array or not. furthermore, it will return TRUE if the given value is found in the given array, and otherwise, it will return FALSE.
The searches haystack for needle using the loose comparison unless strict is set.
Syntax:
Parameter Values
The in_array() function in PHP accepts three parameters, out of which two parameters are compulsory and another one parameter is optional. All three parameters are described below:
Parameters | Description |
---|---|
$val: | It is a required parameter that specifies the element or value to be searched in the given array. This parameter can be of mixed type. mean that, it can be of string type or integer type, or another type. If this parameter is of string type then the search will be performed or executed in a case-sensitive manner. |
$array_name: | This is also a required parameter and it specifies the array in which we wish to search. |
$mode: | It is an optional parameter and is of boolean type. furthermore, It specifies the mode in which we would like to perform the search. If it’s set to TRUE, then the PHP in_array() function searches for the value with the same or equivalent type of value as specified by $val parameter. The default value is FALSE. |
Return Value:
The in_array() function in PHP returns a boolean value i.e, TRUE if the value $val is found in the array otherwise it returns FALSE.
Technical Details
PHP Version: | 4+ |
Return Value: | It will return a TRUE value if the value is found in the array, or FALSE otherwise |
PHP Changelog: | PHP 4.2: The search parameter may currently be an array |
PHP in_array Example
Program:1
In the given below program we search for the value "Cauliflower"
in an array and output some text:
1 2 3 4 5 6 7 |
<?php $veg = array("Capsicum", "Carrot ", "Cauliflower", "Cabbage"); if (in_array("Cauliflower", $veg)) {echo "Match is found";} else {echo "Match is not found";} ?> |
Program:2
This program performs the search using in_array() function in non-strict mode. furthermore, that is, the last parameter $mode is set to false which is the default value. The value to be searched is of string type whereas this value in the array is of integer type not exist in the in_array() function then it returns false as the search is in non-strict mode.
1 2 3 4 5 6 |
<?php $pincode = array(226016, 226010, 226009, 226001, 226011); if (in_array("226003", $pincode)) {echo "match is found";} else{echo "match is not found";} ?> |
Program:3
The given program performs the search using in_array() function in PHP in strict mode. That is, the last parameter $mode is set to true and the function will now also check the type of values.
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 |
<?php $student = array("Pinki", "Shushila", "Tanisha", 77); if (in_array("Pinki", $student, TRUE)) { echo "It's found \n"; } else { echo "It's not found \n"; } if (in_array(77, $student, TRUE)) { echo "It's found \n"; } else { echo "It's not found \n"; } if (in_array("Saumya", $student, TRUE)) { echo "It's found \n"; } else { echo "It's not found \n"; } ?> |
It’s found
It’s not found
Program:4
The given program performs PHP in_array() function with strict example.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $x = array('7.91', 11.11, 21.45); if (in_array('11.11', $x, true)) { echo "'11.11' It's found with strict check\n"; } if (in_array(21.45, $x, true)) { echo "21.45 It's found with strict check\n"; } ?> |
Program:5
The given program performs PHP in_array() with an array as needle.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $x = array(array('a', 'b'), array('c', 'd'), 'f'); if (in_array(array('a', 'b'), $x)) { echo "'ab' was found\n"; } if (in_array(array('m', 'n'), $x)) { echo "'mn' was found\n"; } if (in_array('f', $x)) { echo "'f' was found\n"; } ?> |
‘f’ was found