将多个文件中的内容读取到数组php中


reading content from multiple files into array php

我正在努力解决这个我无法解决的问题。

我有一个顺序文本文件,其中包含如下结构的记录:

    First Name : ......;
    Last Name: ......;
    Contact Details:.....;
    Email Address:.....;
    Enquirer Type:.....;
    Contact Method:....;
    Message:....';
    |----------|
    First Name : ......;
    ..............

我在集装箱内的表格中显示信息时遇到问题

        <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" type="text/css" href="styles/session_Style.css">
    <title>User Session</title>
  </head>
  <body>
    <div class="container">
      <div id="table">
      <?php
$filename = "Contacts.txt";
$content = file_get_contents($filename);
$fileContent = explode('|----------|', $content);
foreach ($fileContent as $number => $contact) {
  echo '<table>';
  echo '<tr><th colspan="2">Contact '.$number.'</th></tr>';
  foreach ($contact as $detail) {
    $detail = explode(':',$detail);
    echo '<tr><td>'.$detail[0].'</td><td>'.$detail[1].'</td></tr>';
  }
  echo '</table>';
}
?>
      </div>      
      <div class="logout">
        <h1>User Details</h1>
        <form method="post" action="">
          <p class="submit"><input type="submit" name="commit" value="Log Out" onclick="document.location.href='home-page.html';"></p>
        </form>
      </div>
    </div>
  </body>
</html>

这是实现你们提供的解决方案的正确方式吗?或者有没有一种方法可以更有效地做到这一点?

尝试:

$fileContent[$filenumber] = explode(';',file_get_contents($filename));

$fileContent是一个以文件号为键的数组。因此,文件contact_1的数据将存储在$fileContent[1]中。explode()将生成一个包含文件内容的数组,用";"分隔。例如,您现在可以使用$fileContent[2][0]访问contact_2的名字。这将返回:"名字:……"


现在来看一下:

foreach ($fileContent as $number => $contact) {
  echo '<table>';
  echo '<tr><th colspan="2">Contact '.$number.'</th></tr>';
  foreach ($contact as $detail) {
    $detail = explode(':',$detail);
    echo '<tr><td>'.$detail[0].'</td><td>'.$detail[1].'</td></tr>';
  }
  echo '</table>';
}