将PHP文本文件转换为数组数组。

huangapple go评论79阅读模式
英文:

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(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;),
        array(&#39;a&#39;, &#39;c&#39;),
        array(&#39;b&#39;));

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=&quot;140724.txt&quot;;
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = &quot;\n&quot;;
$split = explode($remove, $fread);
$array[] = null;
$tab = &quot;\t&quot;;
foreach ($split as $string)
{
    $row = explode($tab, $string);
    array_push($array,$row);
}
echo &quot;&lt;pre&gt;&quot;;
print_r($array);
echo &quot;&lt;/pre&gt;&quot;;

i got this result

Array

(

    [0] =&gt; 

    [1] =&gt; Array
        (
            [0] =&gt; a b c
        )

    [2] =&gt; Array
        (
            [0] =&gt; a b
        )

    [3] =&gt; Array
        (
            [0] =&gt; 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 = &quot;140724.txt&quot;;
$file = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$output = array_map(function($line) { return explode(&quot;\t&quot;, $line); }, $file);

will produce

Array
(
    [0] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; b
            [2] =&gt; c
        )

    [1] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; c
        )

    [2] =&gt; Array
        (
            [0] =&gt; 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=&quot;140724.txt&quot;;
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = &quot;\n&quot;;
$split = explode($remove, $fread);
$array = array();
$tab = &quot; &quot;;
foreach ($split as $string)
{
    $array[] = explode($tab, $string);
}
echo &quot;&lt;pre&gt;&quot;;
print_r($array);
echo &quot;&lt;/pre&gt;&quot;;

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, &quot;r&quot;);
while (($data = fgetcsv($handle, 1000, &quot; &quot;)) !== 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=&quot;140724.txt&quot;;
$fopen = fopen($file, &#39;r&#39;);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = &quot;\n&quot;;
$split = explode($remove, $fread);

$custom_array = array();
$x = 0;
foreach ($split as $value) {
	$value = trim($value);
	if($value != &#39;&#39; || $value != null){
		$inside_explode = explode(&quot; &quot;, $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(&#39;1.txt&#39;, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$result = [];
foreach ($fileData as $row) {
    $splitRow = str_getcsv($row, &#39; &#39;);
    $result[] = $splitRow;
}

echo &#39;&lt;pre&gt;&#39;;
print_r($result);
$fileData = file(&#39;1.txt&#39;, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$result = array_map(function ($row) {
    return str_getcsv($row, &#39; &#39;);
}, $fileData);

echo &#39;&lt;pre&gt;&#39;;
print_r($result);

huangapple
  • 本文由 发表于 2020年1月6日 19:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定