Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
string/int safe inArray implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
radnan committed Jan 21, 2013
1 parent bc7ad45 commit 04b932a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ public static function isHashTable($value, $allowEmpty = false)
return (array_values($value) !== $value);
}

/**
* Checks if a value exists in an array.
*
* Due to "foo" == 0 === TRUE with in_array when strict = false, an option
* has been added to prevent this. When $strict = 0/false, the most secure
* non-strict check is implemented. if $strict = -1, the default in_array
* non-strict behaviour is used.
*
* @param mixed $needle
* @param array $haystack
* @param int|bool $strict
* @return bool
*/
public static function inArray($needle, array $haystack, $strict = false)
{
if (!$strict) {
if (is_int($needle) || is_float($needle)) {
$needle = (string) $needle;
}
if (is_string($needle)) {
foreach ($haystack as &$h) {
if (is_int($h) || is_float($h)) {
$h = (string) $h;
}
}
}
}
return in_array($needle, $haystack, $strict);
}

/**
* Convert an iterator to an array.
*
Expand Down

0 comments on commit 04b932a

Please sign in to comment.