简体   繁体   English

将Text(制表符分隔)转换为PHP关联数组,而不是将此代码转换为CSV吗?

[英]Converting a Text(tab delimited) to PHP associative array, instead of this code for CSV?

I would like to convert my CSV to Array code to a Text(tab delimited) to Array block of code. 我想将CSV代码转换为Array代码,将Text(制表符分隔)转换为Array代码块。 Here is my existing that works for comma's... 这是我现有的适用于逗号的...

$rows = array_map('str_getcsv', $content);
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
   $csv[] = array_combine($header, $row);
}
print_r($csv);

What would be the shortest way to convert for example the 'str_getcsv' to accept tabs over commas as default? 转换“ str_getcsv”以默认接受逗号制表符的最短方法是什么?

$rows = array_map('str_getcsv', $content, array_fill(0, count($content), "\t"));

This puts it in a string with tab delimiters. 这会将其放入带有制表符分隔符的字符串中。 Then you can either explode it to an array or use it as is. 然后,您可以将其分解为数组或按原样使用。

$csv = implode("\t", str_getcsv($file, ","));

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

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