PHP Function In_Array
PHP arrays are very versatile and powerful data types which can be used in a wide range of programming scenarios. As such, there are many functions in the PHP language for working with arrays including adding and removing elements, sorting, searching, merging etc. In this article we will discuss the php function in_array, a very useful and time saving function which can be used to search an array for a given value.
The in_array function searches the given value in an array and returns a boolean value of true or false. The value being searched can be a string or an integer. If the search parameter is a string, it will be compared with values in the array in a case sensitive manner. If the search parameter is an integer, it will be compared with values in an array using loose comparison. If the third parameter strict is set to True, in_array will also check and compare the types of the values being searched.
The in_array function is a very handy and time saving function which can be used to save valuable CPU cycles in large scale projects. When used with a small array it can even be faster than PHP's built in array_search function which takes on average 1.346 seconds to search a 1000 element array for a value. Moreover, if the in_array function is being used in strict mode it can be even faster as it will also compare and check the types of the values being searched.