Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
意外的PHP数组键行为_Php_Arrays_Strpos - Fatal编程技术网

意外的PHP数组键行为

意外的PHP数组键行为,php,arrays,strpos,Php,Arrays,Strpos,好吧,我觉得这很奇怪,但应该有一个解释。下面是发生的情况 此处的代码不应回显任何内容: $str = 'a@gmail.com'; $key = '11111'; echo strpos($str, $key); exit; 。。是的,这正是我得到的,什么都没有。但是 如果要使用包含字符串的$key作为数组的实际键: $str = 'a@gmail.com'; $arr = array('11111' => 'test'); foreach ($arr as $key => $va

好吧,我觉得这很奇怪,但应该有一个解释。下面是发生的情况

此处的代码不应回显任何内容:

$str = 'a@gmail.com';
$key = '11111';
echo strpos($str, $key);
exit;
。。是的,这正是我得到的,什么都没有。但是 如果要使用包含字符串的$key作为数组的实际键:

$str = 'a@gmail.com';
$arr = array('11111' => 'test');
foreach ($arr as $key => $val)
{
    echo 'String: '.$str.'<br>';
    echo 'Key: '.$key.'<br>';
    echo 'Found at position: '.strpos($str, $key);
}
exit;
php在这里找到的是字符串11111,它是字母g 但更令人惊奇的是,位数改变了结果:

$str = 'a@gmail.com';
$arr = array('111' => 'test');
foreach ($arr as $key => $val)
{
    echo 'String: '.$str.'<br>';
    echo 'Key: '.$key.'<br>';
    echo 'Found at position: '.strpos($str, $key);
}
exit;
有这方面的专家吗? 多谢各位

编辑: 这是我的项目中使用的实际代码示例,给出了此类误报:

$email = '[the email of the user here]';
$arr = array(
    // [...]
    '11111' => 'Banned',
    '22222' => 'Banned',
    '33333' => 'Banned',
    // [...]
);
foreach ($arr as $key => $reason)
{
    if (strpos($email, (string)$key) !== false)
    {
        return 'Keyword: '.(string)$key.' found in the user Email address with reason: '.(string)$reason;
    }
}

因此,即使使用变量$key前面的字符串,它也会禁止登录表单中的无辜者使用它,它也可以正常工作。我在字符串中键入$key。PHP函数,用于匹配字符串中的子字符串,而不是整数值。如果您查看文档,就会清楚地看到它

第二个参数:如果指针不是字符串,则将其转换为整数并作为字符的序数值应用


谢谢你的回答,这正是我在我的项目代码循环中所做的,但它仍然会出现误报。所以,问题是,为什么在没有或甚至没有$key之前的字符串的情况下会发生这种情况?真有趣@durduvakis请检查我当前的代码,如果它仍然不工作,那么请在你的帖子中分享不工作的代码。如果你注意到它在单引号中是一个字符串。是的,我明白了,但是strpos本身将它视为整数,所以如果你将它键入string,它将按照你想要的方式开始工作。对,请给我一点时间,找到我的代码,它正好在一小时前因为这个误报而禁止了一个用户。可能是重复的
String: a@gmail.com
Key: 111
Found at position: 9
$email = '[the email of the user here]';
$arr = array(
    // [...]
    '11111' => 'Banned',
    '22222' => 'Banned',
    '33333' => 'Banned',
    // [...]
);
foreach ($arr as $key => $reason)
{
    if (strpos($email, (string)$key) !== false)
    {
        return 'Keyword: '.(string)$key.' found in the user Email address with reason: '.(string)$reason;
    }
}
<?php
ini_set('display_errors', 1);
$str = 'a@gmail.com';
$arr = array('11111' => 'test');
foreach ($arr as $key => $val)
{
    echo 'String: '.$str.'<br>';
    echo 'Key: '.$key.'<br>';
    echo 'Found at position: '.strpos($str, (string)$key);
}