将整个文件添加到PHP数组中

时间:2021-10-08 19:18:35

I have been attempting to write a website that processes names of people and reads them back in a list, so I can do other things with them later. I need them to be alphabetized, and show up with each person on their own line. Kind of like this:

我一直在尝试编写一个处理人名的网站,并将其读回列表中,以便我以后可以用它们做其他事情。我需要它们按字母顺序排列,然后出现在每个人身上。有点像这样:

[1] Doe--John

[1] Doe - John

[2] Washington--George

[2]华盛顿 - 乔治

[3] Zekeman--William

[3]泽克曼 - 威廉

I have created a php script that uses an array to sort the names; it is in the file sort.php. I have created a file for the people, called students.html. These names were fed in from a form.

我创建了一个php脚本,它使用数组对名称进行排序;它位于文件sort.php中。我为人们创建了一个名为students.html的文件。这些名字是从表格中输入的。

To display the name on the webpage, I created a simple php include function.

为了在网页上显示名称,我创建了一个简单的php include函数。

Here is my coding:

这是我的编码:

Sort.php

Sort.php

    <?php

        $filename = "students.html"

        $names = array file( string $filename );
        sort($names);
        foreach ($names as $key => $val) {
              echo "[" . $key . "] " . $val . "\n";
        }

    ?>

Students.html

Students.html

    "Washington--George",
    "Zekeman--William",
    "Doe--John",

Webpage

网页

   <?php include 'sort.php';?>

Everything works perfectly, except the webpage displays this error:

一切都很完美,除了网页显示此错误:

Parse error: syntax error, unexpected T_STRING, expecting '(' in /home/www/the/path/to/my/directory/sort.php on line 3

解析错误:语法错误,意外的T_STRING,期待'(在第3行的/home/www/the/path/to/my/directory/sort.php中)

I have played with it and I cannot figure out how to fix the error. Any assistance would be greatly appreciated!

我玩过它,我无法弄清楚如何修复错误。任何帮助将不胜感激!

Note: I apologize if this makes no sense. I am brand new to this.

注意:如果这没有意义,我道歉。我是全新的。

3 个解决方案

#1


4  

On the end of $filename = "students.html" you need to put a ;

在$ filename =“students.html”结束时你需要放一个;

Edit: also $names = file($filename);

编辑:还$ names = file($ filename);

#2


4  

$names = array file( string $filename );

This is wrong. You need to remove both array and string.

这是错的。您需要删除数组和字符串。

#3


1  

should be

应该

<?php

$filename = "students.html";

$names = file( $filename );
sort($names);
foreach ($names as $key => $val) {
    echo "[" . $key . "] " . $val . "\n";
}

?>

#1


4  

On the end of $filename = "students.html" you need to put a ;

在$ filename =“students.html”结束时你需要放一个;

Edit: also $names = file($filename);

编辑:还$ names = file($ filename);

#2


4  

$names = array file( string $filename );

This is wrong. You need to remove both array and string.

这是错的。您需要删除数组和字符串。

#3


1  

should be

应该

<?php

$filename = "students.html";

$names = file( $filename );
sort($names);
foreach ($names as $key => $val) {
    echo "[" . $key . "] " . $val . "\n";
}

?>