英文:
php text file to array of arrays
问题
我有一个包含以下内容的文本文件:
a b c
a c
b
我想要读取这个文本文件并将其转换成一个数组的数组,格式如下:
array(
array('a', 'b', 'c'),
array('a', 'c'),
array('b')
)
我想要将每一行分割成一个数组,并将每个单词放入数组的索引中。
我尝试了以下代码,但它只是将每一行分割成一个数组,我想要将每个单词放入每一行数组的索引中:
$file = "140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen, filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array = array();
$tab = "\t";
foreach ($split as $string) {
$row = explode($tab, $string);
$array[] = $row;
}
echo "<pre>";
print_r($array);
echo "</pre>";
我得到了以下结果:
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => a b c
)
[2] => Array
(
[0] => a c
)
[3] => Array
(
[0] => b
)
)
如果你想要每个单词都在数组中而不是整行,你可以进一步分割每个行的字符串,将每个单词放入一个新的数组中。如果需要这样的代码,请告诉我。
英文:
i got a text file that contains
a b c
a c
b
i want to read the text file and convert it to array of arrays like this
array(
array('a', 'b', 'c'),
array('a', 'c'),
array('b'));
i want to split each line in an array and each word in a index in the array
i tried this code but it only split each line in a array i want each word in a array of the line
$file="140724.txt";
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array[] = null;
$tab = "\t";
foreach ($split as $string)
{
$row = explode($tab, $string);
array_push($array,$row);
}
echo "<pre>";
print_r($array);
echo "</pre>";
i got this result
Array
(
[0] =>
[1] => Array
(
[0] => a b c
)
[2] => Array
(
[0] => a b
)
[3] => Array
(
[0] => c
))
答案1
得分: 3
你可以通过使用一些内置函数来大大减少代码量。
使用 file()
函数并使用适当的标志将文件读取进来,忽略任何空行,并返回一个数据数组。
然后使用 array_map()
与 explode()
来处理这个数组,并将其拆分成每行的各个部分...
$fileName = "140724.txt";
$file = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$output = array_map(function($line) { return explode("\t", $line); }, $file);
将产生
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => c
)
[2] => Array
(
[0] => b
)
)
英文:
You can reduce the code quite a bit by using some of the inbuilt functions.
Using file()
with the appropriate flags will read the file in, ignoring any empty lines and return an array of data.
Then use array_map()
along with explode()
to process this array and split it into the individual parts per row...
$fileName = "140724.txt";
$file = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$output = array_map(function($line) { return explode("\t", $line); }, $file);
will produce
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => c
)
[2] => Array
(
[0] => b
)
)
答案2
得分: 0
尝试像这样使用。
$file = "140724.txt";
$fopen = fopen($file, r);
$fread = fread($fopen, filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array = array();
$tab = " ";
foreach ($split as $string) {
$array[] = explode($tab, $string);
}
echo "<pre>";
print_r($array);
echo "</pre>";
希望这对您有帮助。
英文:
Try with like this.
$file="140724.txt";
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array = array();
$tab = " ";
foreach ($split as $string)
{
$array[] = explode($tab, $string);
}
echo "<pre>";
print_r($array);
echo "</pre>";
Hope this help to you.
答案3
得分: 0
有几个错误。
首先 - 将 $array
变量初始化为 $array = [];
而不是 $array[] = null;
。
其次,你可以使用 fgetcsv,这会更好。
最后,我认为分隔符是空格
而不是 \t
。
考虑以下代码:
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, " ")) !== FALSE) {
$arr[] = $data;
}
英文:
There are couple of errors.
First - init the $array
var as $array = [];
and not $array[] = null;
.
Second, you can use fgetcsv which will be better.
Last, I think the delimiter is
(white space) and not \t
.
Consider:
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, " ")) !== FALSE) {
$arr[] = $data;
}
答案4
得分: 0
这将在没有空数组的情况下提供完美的输出。稍后感谢我。
$file = "140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen, filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$custom_array = array();
$x = 0;
foreach ($split as $value) {
$value = trim($value);
if ($value != '' || $value != null) {
$inside_explode = explode(" ", $value);
$custom_array[$x] = $inside_explode;
$x++;
}
}
print_r($custom_array);
英文:
This will give perfect output without blank array.Thank me later.
$file="140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$custom_array = array();
$x = 0;
foreach ($split as $value) {
$value = trim($value);
if($value != '' || $value != null){
$inside_explode = explode(" ", $value);
$custom_array[$x] = $inside_explode;
$x++;
}
}
print_r($custom_array);
答案5
得分: 0
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$result = [];
foreach ($fileData as $row) {
$splitRow = str_getcsv($row, ' ');
$result[] = $splitRow;
}
echo '<pre>';
print_r($result);
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$result = array_map(function ($row) {
return str_getcsv($row, ' ');
}, $fileData);
echo '<pre>';
print_r($result);
英文:
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$result = [];
foreach ($fileData as $row) {
$splitRow = str_getcsv($row, ' ');
$result[] = $splitRow;
}
echo '<pre>';
print_r($result);
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$result = array_map(function ($row) {
return str_getcsv($row, ' ');
}, $fileData);
echo '<pre>';
print_r($result);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论