Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.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 如果条件允许,有没有办法处理大量(600+;)的问题_Php - Fatal编程技术网

Php 如果条件允许,有没有办法处理大量(600+;)的问题

Php 如果条件允许,有没有办法处理大量(600+;)的问题,php,Php,我需要比较一个PHP变量和600+静态值 比如说,, $validpostcode是一个变量。有600多个有效邮政编码 现在我需要检查邮政编码列表中是否存在$validpostcode。 比较的最佳方法是什么?如果将邮政编码作为数组,则可以使用in_数组方法,如下所示: $postcodes = array( // Postcodes here ); $validpostcode = "POSTCODE"; if(in_array($validpostcode, $postcodes)){

我需要比较一个PHP变量和600+静态值

比如说,, $validpostcode是一个变量。有600多个有效邮政编码

现在我需要检查邮政编码列表中是否存在$validpostcode。
比较的最佳方法是什么?

如果将邮政编码作为数组,则可以使用in_数组方法,如下所示:

$postcodes = array( // Postcodes here );
$validpostcode = "POSTCODE";

if(in_array($validpostcode, $postcodes)){
    //Postcode is in the array
}
祝你好运


还有,如果你每次都要写邮政编码,然后将它们存储在数据库中,然后使用MYSQL查询将它们全部捕获到一个数组中,然后使用上述方法。

如果你将邮政编码作为一个数组,你可以使用in_数组方法,如下所示:

$postcodes = array( // Postcodes here );
$validpostcode = "POSTCODE";

if(in_array($validpostcode, $postcodes)){
    //Postcode is in the array
}
祝你好运


还有,如果每次都要编写邮政编码,那么先将它们存储在数据库中,然后使用MYSQL查询将它们全部捕获到数组中,然后使用上述方法。

我能想到的最快方法是使用
isset()
。当然,您可以在数组中使用
数组相交
,但它们比只检查是否有键要重

给你:

$codes = array_fill_keys(array('code1', 'code2'), 0);
$tocheck = 'code1';
if (isset($codes[$tocheck])) {
      echo 'valid';
} else {
      echo 'nope';
}

执行时间测试比较参考:

我能想到的最快的方法是使用
isset()
。当然,您可以在数组中使用
数组相交
,但它们比只检查是否有键要重

给你:

$codes = array_fill_keys(array('code1', 'code2'), 0);
$tocheck = 'code1';
if (isset($codes[$tocheck])) {
      echo 'valid';
} else {
      echo 'nope';
}

执行时间测试比较参考:

绝对
in_array()
您可以将邮政编码存储在MySQL表中,我猜邮政编码不会经常更改,您在页面上经常这样做,如果Tristan有正确的想法,请将邮政编码存储在数据库中,然后检查是否存在。当然
在_array()中
你可以将邮政编码存储在MySQL表中,我猜邮政编码不会经常更改,你在页面上经常这样做,如果特里斯坦的想法正确,将邮政编码存储在数据库中,然后检查是否存在。谢谢,克里斯,我有CSV格式的邮政编码列表,我刚刚将它们转换成一个数组,效果非常好。谢谢,克里斯,我有CSV格式的邮政编码列表,我刚刚将它们转换成一个数组,效果非常好。