note 105178 added to function.in-array
| From: | Ray dot Paseur at Gmail dot com | Date: | Mon, 01 Aug 2011 16:20:14 +0000 |
| Subject: | note 105178 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182073@lists.php.net to get a copy of this message | ||
Be VERY careful about testing for strings in arrays that might also contain integers. In this
snippet, the range() function turns the numeric values into integers, and loose data typing finds a
match with zero.
Outputs:
PHP in_array() FOUND @#%?!!
@#%?!! == 0 AT POSITION 26 BECAUSE OF LOOSE TYPE
<?php // RAY_temp_in_array.php
error_reporting(E_ALL);
echo "<pre>";
// THE ALLOWABLE SELECTIONS
$alpha = range('A','Z');
$alpha = array_merge($alpha, range('0', '9'));
// LOOK FOR A BOGUS SELECTION
$str = '@#%?!!';
if (in_array($str, $alpha))
{
echo "PHP in_array() FOUND $str ";
}
else
{
echo "$str IS NOT IN ARRAY";
}
// SEARCH THE ARRAY FOR THE KEY OF THE STRING
$key = array_search($str, $alpha);
echo "<br/>$str == $alpha[$key] AT POSITION $key";
if ($str == 0) echo " BECAUSE OF LOOSE TYPE";
----
Server IP: 69.147.83.197
Probable Submitter: 98.169.58.122
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/105178
Del: integrated -- https://master.php.net/note/delete/105178/integrated
Del: useless -- https://master.php.net/note/delete/105178/useless
Del: bad code -- https://master.php.net/note/delete/105178/bad+code
Del: spam -- https://master.php.net/note/delete/105178/spam
Del: non-english -- https://master.php.net/note/delete/105178/non-english
Del: in docs -- https://master.php.net/note/delete/105178/in+docs
Del: other reasons-- https://master.php.net/note/delete/105178
Reject -- https://master.php.net/note/reject/105178
Search -- https://master.php.net/manage/user-notes.php