简体   繁体   English

PHP in_array无法正常工作

[英]PHP in_array not working correctly

I am creating this array from a posted textarea and splitting the lines: 我正在从发布的textarea创建此数组并拆分行:

$ignored = array();
foreach(explode("\n", $_POST["ignored"]) as $ignored2) {
    $ignored[] = $ignored2;
    echo $ignored2.'<br>';
}

then i have a while loop where i check if any of the array items are in a variable within the while loop: 然后我有一个while循环,在这里我检查while循环内是否有任何数组项在变量中:

while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    //check if the description is anything in the ignore array
    if(in_array($data[6], $ignored)) {
        echo 'ignore';
    } else {
        echo 'dont ignore';
    }
}

I put the following in the textarea (one on each line): 我将以下内容放在文本区域中(每行一个):

SIP Trunk: ST17830T001 (200 channels)
SIP Trunk: ST17830T002 (1 channels)

but its only echoing 'ignore' once and its not ignoring the other item (they both exist in the $data[6] variable 但它只回显一次“忽略”,并且不忽略其他项目(它们都存在于$ data [6]变量中

Trim the data from the textarea: 修剪文本区域中的数据:

$ignored = array();
foreach(explode("\n", $_POST["ignored"]) as $ignored2) {
    $ignored[] = trim($ignored2);
    echo $ignored2.'<br>';
}

Also trim the variable you're testing: 还修剪您要测试的变量:

if (in_array(trim($data[6]), $ignored)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM