Php 数组无法接受数据

Php 数组无法接受数据,php,arrays,Php,Arrays,概念:用户输入其邮政编码(EXA1 2PL或EX1 2AP)我们修剪最后3个字符,并保留邮政编码的开头(EXA1或EX1);现在,我们根据数组检查数据,如果匹配,则执行其他操作 在处理用户数据时,我总是得到一个数组输出,其中没有任何内容。下面是我正在使用的代码 $op_postcodes = '"PH3,PH4,PH5"'; //Data has been cut for stack purposes however stays in same format //with one " at th

概念:用户输入其邮政编码(EXA1 2PLEX1 2AP)我们修剪最后3个字符,并保留邮政编码的开头(EXA1EX1);现在,我们根据数组检查数据,如果匹配,则执行其他操作

在处理用户数据时,我总是得到一个数组输出,其中没有任何内容。下面是我正在使用的代码

$op_postcodes = '"PH3,PH4,PH5"';
//Data has been cut for stack purposes however stays in same format
//with one " at the start of the string and one at the end

$op_postcodes = str_replace(',', '","', $op_postcodes);
$op_postcodes = array($op_postcodes);
此输出:
数组
,无法与用户输入匹配


如果你不明白我在说什么,我很抱歉。我的英语不太好。

如前所述,如果使用
explode
你将得到一个包含多个元素的数组,这就是你当前的方法。但是,我不知道为什么要像这样将数组值封装在引号中

$op_postcodes = '"PH3,PH4,PH5"';
$op_postcodes = str_replace(',', '","', $op_postcodes);
$op_postcodes = explode(',', $op_postcodes);

echo in_array( '"PH4"', $op_postcodes ) ? 'Found it' : 'Ooops, could not find it';
除非特别需要将数组值封装在引号中,否则对上述内容的细微修改可能是:

$search_user_value='PH4';

$op_postcodes = '"PH3,PH4,PH5"';
$op_postcodes = str_replace('"', '', $op_postcodes );
$op_postcodes = explode(',', $op_postcodes);
echo in_array( $search_user_value, $op_postcodes ) ? 'Found it' : 'Ooops, could not find it';

使用
分解
创建array@explode你能不能把这个作为一个答案,这样我可以给你信用,如果它工作的话?封装在引号中可能只是从常规数组语法,数组(“PH3”,“PH4”,“PH5”)等混淆。谢谢你的帮助,还有@MatsLindh,这就是我试图做的,但是我已经移除了这个,现在它工作正常了!