PHP Arrays, one of the interesting topics in PHP, in learning part of a view, since, it includes several direct PHP functions to work with arrays. In this article, we took one of those functions, PHP in_array(), to learn about it in detail.
In PHP, in_array() is used to check whether a value or subarray is part of a given master array or not. If the given value or sub-array exists in the master array, then in_array() will return TRUE, or false otherwise.
The following syntax shown will help us to use the in_array function in a PHP program. This function includes three parameters, where the third one is optional.
<?php
in_array($data_to_search, $master_array, $strict_search_flag);
?>
Note:
<?php
echo in_array('3', array(
true,
"one",
"two",
"three"
));
?>
This program will return true, even though there is no such entry in the master array. So, strict matching will solve this problem, by keeping the third parameter as TRUE.
PHP includes some other functions to search for a keyword or string over a master array as like as PHP in_array. These functions are,
We have seen that PHP in_array() function returns TRUE if there is match found between given data and the values of the master array. Similarly, array_key_exists() will do the same for searching keys or indices if the master array is an associative array.
Let us check the behavior of this function by using the following PHP code.
<?php
$master_assoc_array = array(
"id" => 1000,
"name" => "PHP"
);
echo array_key_exists("name", $master_assoc_array);
?>
In the above program, array_key_exists() will return TRUE, since, the given master associative array includes such a key, as we have specified for the key search. And, if we search keys with respect to the nonassociative array, then we can find for numeric indices, which will be associated with each element of a PHP nonassociative array by default.
This is equivalent to a PHP function like in_array() but differs from what it is returning. If any match is found between given data and values of the master array, then, PHP array_search() will return the corresponding index of the entry with which the match is found, whereas, in_array() returns the boolean value TRUE, instead.
The following PHP code will return the index of the given string with which it is associated in the master array.
<?php
$master_array = array(
"one",
"two",
"three"
);
echo "Array index of 'three' is " . array_search("three", $master);
?>
Since, the given master array is a non-associative array, as per the default numeric indices associated with the element of $master_array, the PHP print statement will return the following output to the browser.
Array index of 'three' is 2
Other than returning the index of the matched entry, array_search() is identical to PHP in_array, since it accepts the same set of three arguments, searches in case sensitive manner and allows strict matching. So, we can use any of these functions based on the requirements, like, if we want to get a key index or simply want to check if a match is found.
Hi Vincy,
I am following your blog daily and your tutorials are simplistic. Every tutorial has something new to learn.
You are covering the topics in-depth and complete. Thanks and keep writing.
Hi Princy,
Thanks for your comments.
Thanks. Easy to understand.
Welcome.
absolutely good tutorial
Thank you Luckmoshy.