Syntax
The first two arguments of this function are mandatory and the third argument is optional. The first argument contains the value that will be searched. The second argument contains the array in which the search will be done. The third argument is optional that is used to define the type of the search. The default value of the third argument is false that is used to search the element based on the search value only. If the value of the third argument is set to true, then the search will be performed based on the search value and type.
Uses of in_array() Function
Different uses of the in_array() function have shown in this part of the tutorial by using multiple examples.
Example-1: Search Value in an Array
Create a PHP file with the following script to search a number into an array of numeric values. The search value will be taken from the URL query parameter named ‘n’. The in_array() function will return true if the search value exists in the array, otherwise it will return false.
//Declare an array of numbers
$n_array = array(89, 34, 56, 23, 90, 45, 12, 67);
//Check the searching number is given in the URL or not
if (isset($_GET['n']))
{
//Read the number passed in the URL
$num = $_GET['n'];
//Check the number exist in the array or not
if (in_array($num, $n_array)) {
echo "<h3>$num exists in the array.</h3>";
} else {
echo "<h3>$num does not exist in the array.</h3>";
}
}
else
//Print message if no URL query parameter named 'n' with value has passed
echo "<h3>No number has given.</h3>";
?>
Output:
The following output will appear after executing the above script if no search value is given in the URL address.
The following output will appear after executing the above script for the search value, 90 that exists in the array.
The following output will appear after executing the above script for the search value, 900 that does not exist in the array.
Example-2: Search Value with Type in an Array
Create a PHP file with the following script to search a value with the type in an array of mixed data. The array contains the number, string, and boolean values. The searchVal() function has been used in the script to search the particular value with type in the array. The function has been called three times to search three values in the array.
//Declare function to Search item in the array
function searchVal($src, $arr)
{
if (in_array($src, $arr, TRUE))
echo "The <b>$src</b>exist in the array.<br/>";
else
echo "The <b>$src</b>does not exist in the array.<br/>";
}
//Declare an array of mix values
$mixArray = array("Mango", 100, "cake", True, "78");
//Print the array values
print_r($mixArray);
echo "<br/>";
/*
Search different types of values by calling
the searchVal() multiple times
*/
$search = "Cake";
searchVal($search, $mixArray);
$search = "100";
searchVal($search, $mixArray);
$search = True;
searchVal($search, $mixArray);
?>
Output:
The following output will appear after executing the above script. According to the output, ‘Cake’ and ‘cake’ values are not equal for case-sensitive search, “100” and 100 are not equal for the type, True and True are equal.
Example-3: Search One-Dimensional Array in the Two-Dimensional Array
Create a PHP file with the following script that will search a one-dimensional array in a two-dimensional array. The searchVal() function has been used in the script to search an array inside another array. This function will take a one-dimensional array in the first argument that will be searched and a two-dimensional array in the second argument in which a one-dimensional array will be searched.
//Declare an two-dimensional array
$employees = array(array('John','CEO', '[email protected]'),
array('Farhan','MD', '[email protected]'),
array('Maruf','Manager', '[email protected]'));
/*
Search two arrays in the two-dimensional array
by calling searchVal() function two times
*/
$src_array = array('Farhan','MD', '[email protected]');
searchVal($src_array, $employees);
$src_array = array('john','CEO', '[email protected]');
searchVal($src_array, $employees);
//Declare function to Search array in the array
function searchVal($src_arr, $main_arr)
{
if (in_array($src_arr, $main_arr))
echo "The employee information exists.<br/>";
else
echo "The employee information does not exist.<br/>";
}
?>
Output:
The following output will appear after executing the above script. Here, the in_array() function has returned true for the first searching array and false for the second searching array.
Example-4: Search Object in an Array of Objects
Create a PHP file with the following script that will search an object into an array of objects. A class named Book has been declared in the script that has a constructor to initialize two class variables. An array of objects has been declared in which three objects of the class have been defined as the array values. Next, the particular object has been searched in the array.
//Declare the class
class Book
{
public $book;
public $author;
function __construct($bookname, $authorname)
{
$this->book = $bookname;
$this->author = $authorname;
}
}
//Declare an array of objects
$books = array(new Book('The Joy of PHP', 'Alan Forbes'),
new Book('Modern PHP: New Features and Good Practices', 'Josh Lockhart'),
new Book('PHP Solutions: Dynamic Web Design Made Easy', 'David Powers'));
//Search an object in the array
if (in_array(new Book('The Joy of PHP', 'Alan Forbes'), $books))
echo "<br/><b>The Book information exists.</b><br/>";
else
echo "<br/><b>The Book information does not exist.</b><br/>";
Output:
The following output will appear after executing the above script. Here, the in_array() function has returned true because the searching object exists in the array.
Conclusion
The various uses of the in_array() function have been explained in this tutorial by using very simple examples to help the PHP users to know the purpose of using this function and apply it properly in the script.