数组键不加起来但使键从 0 到 9 3 次
Array Keys do not add up but makes keys go from 0 to 9 3 times
当我将 4 个数组组合成 1 个数组时,它使键 [0] 到 [9] 4 次,而不是从 [0] 到 [34]。
我正在使用的文件是一个包含以下内容的 .txt 文件(只有 4 行随机文本):
gghondxs
vishfgfh
magiedhv
bvmdomvb
<form method="post" action="index.php" enctype="multipart/form-data">
<input class="file-choise" name="file" type="file" />
<input class="upload-button" type="submit" value="Upload Bestand" />
</form>
$file = fopen($_FILES['file']['tmp_name'], "rb");
$linearray = array();
$workingarray = array();
while(! feof($file)) {
$line = fgets($file);
echo "$line<br>";
$linearray[] = (str_split("$line"));
}
$workingarray[] = array($linearray,"endline");
print_r($workingarray);
我得到的输出如下;
Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => g [1] => g
[2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => [9] => )
[1] => Array ( [0] => v [1] => i [2] => s [3] => h [4] => f [5] => g
[6] => f [7] => h [8] => [9] => ) [2] => Array ( [0] => m [1] => a [2]
=> g [3] => i [4] => e [5] => d [6] => h [7] => v [8] => [9] => ) [3] => Array ( [0] => b [1] => v [2] => m [3] => d [4] => o [5] => m [6] => v [7] => b [8] => [9] => ) [4] => Array ( [0] => ) ) [1] => endline ) )
我希望它看起来像这样
Array ( [0] => Array [0] => g [1] => g [2] => h [3] => o [4] => n [5]
=> d [6] => x [7] => s [8] => v [9] => i [10] => s [11] => h [12] => f [13] => g [14] => f [15] => h [16] => m [17] => a [18] => g [19] => i
[20] => e [21] => d [22] => h [23] => v [24] => [25] => b [26] => v
[27] => m [28] => d [29] => o [30] => m [31] => v [32] => b [33] =>
[34] => endline ) )
有人可以帮我实现这个吗?
谢谢
$workingarray = call_user_func_array('array_merge', $workingarray);
可以使用数组合并吗?
$linearray[] = array_merge( $linearray, str_split("$line") );
<?php
$file = fopen($_FILES['file']['tmp_name'], "rb");
$linearray = array();
$workingarray = array();
while(! feof($file)) {
$line = fgets($file);
echo "$line<br>";
$line = str_split(trim($line));
$linearray = array_merge($linearray, $line);
// $linearray = array_merge($linearray, $line, array("endline")); Include endline after each actual line.
}
$workingarray[] = $linearray;
var_dump($workingarray);
只需在现有代码中添加一行:-
$workingarray[] = array($linearray,"endline");
// This is a new line
$workingarray = array_values($workingarray);
print_r($workingarray);
array_values() 将在您的数组中重新生成从 0 开始的键索引。
当我将 4 个数组组合成 1 个数组时,它使键 [0] 到 [9] 4 次,而不是从 [0] 到 [34]。
我正在使用的文件是一个包含以下内容的 .txt 文件(只有 4 行随机文本):
gghondxs
vishfgfh
magiedhv
bvmdomvb
<form method="post" action="index.php" enctype="multipart/form-data">
<input class="file-choise" name="file" type="file" />
<input class="upload-button" type="submit" value="Upload Bestand" />
</form>
$file = fopen($_FILES['file']['tmp_name'], "rb");
$linearray = array();
$workingarray = array();
while(! feof($file)) {
$line = fgets($file);
echo "$line<br>";
$linearray[] = (str_split("$line"));
}
$workingarray[] = array($linearray,"endline");
print_r($workingarray);
我得到的输出如下;
Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => g [1] => g [2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => [9] => ) [1] => Array ( [0] => v [1] => i [2] => s [3] => h [4] => f [5] => g [6] => f [7] => h [8] => [9] => ) [2] => Array ( [0] => m [1] => a [2] => g [3] => i [4] => e [5] => d [6] => h [7] => v [8] => [9] => ) [3] => Array ( [0] => b [1] => v [2] => m [3] => d [4] => o [5] => m [6] => v [7] => b [8] => [9] => ) [4] => Array ( [0] => ) ) [1] => endline ) )
我希望它看起来像这样
Array ( [0] => Array [0] => g [1] => g [2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => v [9] => i [10] => s [11] => h [12] => f [13] => g [14] => f [15] => h [16] => m [17] => a [18] => g [19] => i [20] => e [21] => d [22] => h [23] => v [24] => [25] => b [26] => v [27] => m [28] => d [29] => o [30] => m [31] => v [32] => b [33] => [34] => endline ) )
有人可以帮我实现这个吗?
谢谢
$workingarray = call_user_func_array('array_merge', $workingarray);
可以使用数组合并吗?
$linearray[] = array_merge( $linearray, str_split("$line") );
<?php
$file = fopen($_FILES['file']['tmp_name'], "rb");
$linearray = array();
$workingarray = array();
while(! feof($file)) {
$line = fgets($file);
echo "$line<br>";
$line = str_split(trim($line));
$linearray = array_merge($linearray, $line);
// $linearray = array_merge($linearray, $line, array("endline")); Include endline after each actual line.
}
$workingarray[] = $linearray;
var_dump($workingarray);
只需在现有代码中添加一行:-
$workingarray[] = array($linearray,"endline");
// This is a new line
$workingarray = array_values($workingarray);
print_r($workingarray);
array_values() 将在您的数组中重新生成从 0 开始的键索引。