如何将长串数字从CSV文件转换为数组?

时间:2021-01-08 21:32:42

I have a CSV file with around 800 number of 5 digits each. I want to create an array with each element being one number of five digits. I am not very proficient in Excel, but I copied the list of numbers and created a string of the number. However, because I copied if from an Excel file, when I pasted it into my PHP code, it pasted one number per line, creating a string that is ~800 lines long. That is fine, but now that I want to create the array using the $cbsa_keys_as_array = str_split($cbsa_keys, 5); function, php counts each line break as a character, so that my second element is only four digits long, throwing everything off.

我有一个CSV文件,每个包含大约800个5位数字。我想创建一个数组,每个元素是一个五位数。我不是很精通Excel,但我复制了数字列表并创建了一个数字字符串。但是,因为我从Excel文件中复制了if,当我将其粘贴到我的PHP代码中时,它每行粘贴一个数字,创建一个大约800行的字符串。这很好,但现在我想使用$ cbsa_keys_as_array = str_split($ cbsa_keys,5)创建数组;函数,php将每个换行符计为一个字符,这样我的第二个元素只有四位数字,将所有内容都抛弃。

2 个解决方案

#1


0  

Split it on the newlines with explode:

通过爆炸将其拆分在换行符上:

$cbsa_keys_as_array = explode("\n", $cbsa_keys);

#2


0  

Remove the linebreaks and then split it.

删除换行符然后拆分它。

$cbsa_keys = preg_replace("/\r|\n/", "", $cbsa_keys);
$cbsa_keys_as_array = str_split($cbsa_keys, 5);

#1


0  

Split it on the newlines with explode:

通过爆炸将其拆分在换行符上:

$cbsa_keys_as_array = explode("\n", $cbsa_keys);

#2


0  

Remove the linebreaks and then split it.

删除换行符然后拆分它。

$cbsa_keys = preg_replace("/\r|\n/", "", $cbsa_keys);
$cbsa_keys_as_array = str_split($cbsa_keys, 5);