php将文件从文件插入到数组中

发布于 2025-02-12 18:25:04 字数 2019 浏览 0 评论 0原文

我有一个带有城市和人口的文本文件以及一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-02-19 18:25:04

发布解决方案:

<?php
 $fileHandler = fopen("Info_array.txt", "rb");
 $data = array();

 while (!feof($fileHandler) ) {
 // read file row
 $row = fgets($fileHandler);

 // explode array by tab
 $temp = explode('\t', $row);
 $data += [$temp[0] => $temp[1]];
 //array_push($data, $temp[0], $temp[1]);
 }

 fclose($fileHandler);

 arsort($data);
?>
<table class="heavyTable">
    <thead>
        <tr>
            <th>City</th>
            <th>Population</th>
        </tr>
    </thead>
    <tbody>
<?php foreach( $data as $city => $pop): ?>
<tr>
<td><?= $city ?></td>
<td><?= $pop ?></td>
</tr>
<?php endforeach; ?>
   </tbody>
  </table>

Posting solution:

<?php
 $fileHandler = fopen("Info_array.txt", "rb");
 $data = array();

 while (!feof($fileHandler) ) {
 // read file row
 $row = fgets($fileHandler);

 // explode array by tab
 $temp = explode('\t', $row);
 $data += [$temp[0] => $temp[1]];
 //array_push($data, $temp[0], $temp[1]);
 }

 fclose($fileHandler);

 arsort($data);
?>
<table class="heavyTable">
    <thead>
        <tr>
            <th>City</th>
            <th>Population</th>
        </tr>
    </thead>
    <tbody>
<?php foreach( $data as $city => $pop): ?>
<tr>
<td><?= $city ?></td>
<td><?= $pop ?></td>
</tr>
<?php endforeach; ?>
   </tbody>
  </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文