我正在用php编写一个基本函数,但是发生了一些非常奇怪的事情。关键字具有关键字“$ary”的值,当I var_dump($ary)时,它显示关键字值存在。但是,当我以$ary‘’Keyword‘为目标时,它返回null。例如,看一下下面的代码。
echo var_dump($ary);
echo var_dump($ary['Keyword']) . "<br><br><br>";它将输出
myinfopleasedontsteal/php:354:
array (size=2)
'Keyword' => string 'Acceptance' (length=10)
'' => string '' (length=0)
myinfopleasedontsteal/php:355:null为什么$ary“‘Keyword”返回null而不是字符串?值得一提的是,$ary是由csv文档形成的列表中的一个元素。
编辑: var_dump(array_keys($ary))打印以下内容
array (size=2)
0 => string 'Keyword' (length=10)
1 => string '' (length=0)下面是将CSV转换为数组的代码
function LOAD_CSV2ARRAY($filename='') // Converts a CSV to an associated 2d array
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 0, ",")) !== FALSE)
{
if(!$header)
{
$header = $row; // tried replacing this with the line commented out below
// $header = array_map('trim',$row);
}
else
{
if(count($header)!=count($row)){ continue; }
// $header = array_map('trim',$header); tried adding this
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}发布于 2020-07-08 04:40:56
我找出了问题的起因。我将excel电子表格导出为UTF8编码的CSV。如果我将其导出为纯CSV,则密钥不包含任何无法打印的字符
https://stackoverflow.com/questions/62783118
复制相似问题