Note 005 PHP gets CSV content

黄舟
Release: 2023-03-04 08:56:01
Original
1043 people have browsed it

不时会需要用到PHP获取CSV文件内内容,而不时又意味着会有一段时间间隔,然后就会发现,有些细节可能忘记了。

然后就是度娘、谷歌,再之后就觉得,咦,我又在做重复的事情了。

所以得把这个虽然简单,但是还是偶尔需要查查资料的东西给记录下来了。

CSV 文件内格式如下:

Note 005 PHP gets CSV content

PHP端代码如下:

$mapDept = array();
$handle = fopen(dirname(__FILE__).'/temp/itfPsDept.csv', 'r');
while ($data = fgetcsv($handle)) {
    $data = array_filter($data);    
    if (!empty($data)) {
        count($data) == 2 &&$mapDept[trim($data[1])] = trim($data[0]);
    }
}
fclose($handle);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($mapDept);
exit();
Copy after login

得到的结果:

Note 005 PHP gets CSV content

 以上就是笔记005 PHP获取CSV内容的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template