PHP | in_array() Function


The in_array() function is an inbuilt function in PHP. The in_array() function is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

syntax:

bool in_array ( $val, $array_name ,$mode )

Parameters: The in_array() function accepts three parameters, out of which two are compulsory and other one is optional. All three parameters are described below:



  1. $val: This is a required parameter which specifies the element or value to be searched in the given array. This parameter can be of mixed type i.e, it can be of string type or integer type or any other type. If this parameter is of string type then the search will be performed in case-sensitive manner.
  2. $array_name: This is a required parameter and it specifies the array in which we want to search.
  3. $mode: This is an optional parameter and is of boolean type. This parameter specifies the mode in which we want to perform the search. If it is set to TRUE, then the in_array() function searches for the value with the same type of value as specified by $val parameter. The default value of this parameter is FALSE.

Return Value: The in_array() function returns a boolean value i.e, TRUE if the value $val is found in the array otherwise it returns FALSE.

Below programs illustrate The in_array() function in PHP:

Program 1: The below program performes the search using in_search() function in non-strict mode. That is, the last parameter $mode is set to false which is it’s default value. The value to be searched is of string type whereas this value in the array is of integer type still the in_array() function returns true as the search is in non-strict mode.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$marks = array(100, 65, 70, 87);
  
if (in_array("100", $marks))
  {
  echo "found";
  }
else
  {
  echo "not found";
  }
?>

chevron_right


Output:

found

Program 2: The below program performes the search using in_array() function in strict mode. That is, the last parameter $mode is set to true and the function will now also check the type of values.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$name = array("ravi", "ram", "rani", 87);
  
if (in_array("ravi", $name, TRUE))
  {
  echo "found \n";
  }
else
  {
  echo "not found \n";
  }
  
if (in_array(87, $name, TRUE))
  {
  echo "found \n";
  }
else
  {
  echo "not found \n";
  }
  
if (in_array("87", $name, TRUE))
  {
  echo "found \n";
  }
else
  {
  echo "not found \n";
  
?>

chevron_right


Output:

found
found
not found

Reference:
http://php.net/manual/en/function.in-array.php



My Personal Notes arrow_drop_up

Sudipta sahana BTech 2nd year Cse student

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.