User Contributed Notes 50 notes

Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.

In a high-voted example, an array is given that contains, amongst other things, true, false and null, against which various variables are tested using in_array and loose checking.

It impossible to receive false as a return value from in_array using loose checking if your arrays contains both the constants true and false. You might understandably trip over this (or the inverse - passing boolean true to check against an array of e.g. non-empty strings), but it's certainly not counter intuitive and makes perfect sense.

Since sometimes in_array returns strange results - see notes above.I was able to find value in array by this quite a simple function;<?php/*** $find <mixed> value to find* $array<array> array to search in*/

If you search for numbers, in_array will convert any strings in your array to numbers, dropping any letters/characters, forcing a numbers-to-numbers comparison. So if you search for 1234, it will say that '1234abcd' is a match. Example:

If third parameter is not set to Strict then, the needle is found in haystack eventhought the values are not same. the limit behind the decimal seems to be 6 after which, the haystack and needle match no matter what is behind the 6th.

A first idea for a function that checks if a text is in a specific column of an array.It does not use in_array function because it doesn't check via columns.Its a test, could be much better. Do not use it without test.

One last detail, I moved my functions around in the script to make sure there was no memory caching, reiteration op or whatever, it's all the same. in_array is the slowest and the use of isset($array[$NEEDLE]) the fastest.

Kelvin's case-insensitive in_arrayi is fine if you desire loose typing, but mapping strtolower onto the array will (attempt to) cast all array members to string. If you have an array of mixed types, and you wish to preserve the typing, the following will work:

If made a in_array function that checks if the specified key matches. It works recursivly so it doesn't matter how deep your input array is.<?phpfunction myInArray($array, $value, $key){//loop through the arrayforeach ($array as $val) {//if $val is an array cal myInArray again with $val as array inputif(is_array($val)){ if(myInArray($val,$value,$key)) return true; }//else check if the given key has $value as valueelse{ if($array[$key]==$value) return true; } } return false; }?>

strict If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

I think a better explanation would be :-

strict

If the third parameter, strict, is set to TRUE, then the in_array() function will do a strict comparison of values and types between the needle and haystack.

The default setting for the third parameter, strict, is FALSE, here the in_array() function will do type juggling in an attempt to match a value when a type is not the same between the needle and haystack.