简体   繁体   English

csv字符串比较不起作用

[英]csv string comparison not working

I'm checking if a string appears in an array filled with fgetcsv() , I've checked the content and the string it's contained at the array but in_array() returns false 我正在检查字符串是否出现在填充有fgetcsv()的数组中,我已经检查了内容及其包含在数组中的字符串,但是in_array()返回false

function checkFieldCSV($index,$code){
    $codes  = array();
    if (($handle = fopen("./csv/file.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
            array_push($codes, explode(';', $data[0])[$index] );
        }
        fclose($handle);
    }
    $codes = removeCommas($codes);
    var_dump($codes);
    var_dump($code);
    return in_array($code, $codes);
}

Output: 输出:

bool(false)

I think you try to search in a two dimensional array. 我认为您尝试在二维数组中进行搜索。

array_push($codes, explode(';', $data[0])[$index] );

this will result in $codes[0][0...n] so in array wont work. 这将导致$ codes [0] [0 ... n],所以在数组中将无法工作。

try the following 尝试以下

$found = false;
foreach($codes as $key=>$value){
   $found = in_array($code, $value);
}
return $found;

Code is not tested, let me know if this has helped you. 代码未经测试,请告诉我这是否对您有所帮助。

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

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