php将文件从文件插入到数组中
我有一个带有城市和人口的文本文件以及一个PHP程序,该文件应读取文本文件并将文本插入数组,同时考虑到城市应该是关键,并且人口是数组的价值,并且该程序必须对数组按值并以表格式打印其键和值。
示例数据:
Tokyo 39105000
Jakarta 35362000
Delhi 31870000
Manila 23971000
Sao Paulo 22495000
Seoul 22394000
Mumbai 22186000
Shanghai 22118000
Mexico City 21505000
Guangzhou 21489000
这是代码:
<?php
$fileHandler = fopen("Info_array.txt", "rb");
$data = [];
while (!feof($fileHandler) ) {
// read file row
$row = fgets($fileHandler);
$index = count($data);
// explode array by space
$data[$index] = explode(' ', $row);
// explode data[$index][0] by '.', then insert into array in index 0 & 1
array_splice($data[$index], 0, 1, explode('.', $data[$index][0]));
}
// sort array by array index 1
usort($data, function ($prev, $next) {
if ($prev[1] == $next[1]) {
return 0;
}
return ($prev[1] < $next[1]) ? -1 : 1;
});
fclose($fileHandler);
?>
<table class="heavyTable">
<thead>
<tr>
<th>City</th>
<th>Population</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $item): ?>
<tr>
<td><?= $item[0] . '.' . $item[1] ?></td>
<td><?= $item[2] ?></td>
<td><?= $item[1] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
我当前会遇到错误:
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
关于如何执行此操作的任何建议将不胜感激,我现在几天都在挣扎。
I have a text file with cities and population and a php program that should read the text file and insert the text into array while taking into account that the city should be key and the population is value of the array and the program has to sort the array by the value and to print its keys and values in table format.
Example data:
Tokyo 39105000
Jakarta 35362000
Delhi 31870000
Manila 23971000
Sao Paulo 22495000
Seoul 22394000
Mumbai 22186000
Shanghai 22118000
Mexico City 21505000
Guangzhou 21489000
This is the code:
<?php
$fileHandler = fopen("Info_array.txt", "rb");
$data = [];
while (!feof($fileHandler) ) {
// read file row
$row = fgets($fileHandler);
$index = count($data);
// explode array by space
$data[$index] = explode(' ', $row);
// explode data[$index][0] by '.', then insert into array in index 0 & 1
array_splice($data[$index], 0, 1, explode('.', $data[$index][0]));
}
// sort array by array index 1
usort($data, function ($prev, $next) {
if ($prev[1] == $next[1]) {
return 0;
}
return ($prev[1] < $next[1]) ? -1 : 1;
});
fclose($fileHandler);
?>
<table class="heavyTable">
<thead>
<tr>
<th>City</th>
<th>Population</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $item): ?>
<tr>
<td><?= $item[0] . '.' . $item[1] ?></td>
<td><?= $item[2] ?></td>
<td><?= $item[1] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I currently get error:
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Warning: Undefined array key 1 in C:\xampp\htdocs\phpexercise.php on line 20
Any advices on how to do this will be very much appreciated, i'm struggling from a few days now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布解决方案:
Posting solution: