PHP:将数组输出到表中

PHP:将数组输出到表中,php,arrays,sorting,html-table,explode,Php,Arrays,Sorting,Html Table,Explode,对于我的PHP项目,我需要将排序后的数组打印到表中。我们不能使用预先制作的排序函数来实现这一点,我们被指示制作我们自己的排序函数。排序工作正常,但它不会以我们需要的方式进入html表格格式 <?php $fileInput = "guestBook.txt"; $fileInputInData = file_get_contents($fileInput); $array=explode("\n", $fileInputInData); for($

对于我的PHP项目,我需要将排序后的数组打印到表中。我们不能使用预先制作的排序函数来实现这一点,我们被指示制作我们自己的排序函数。排序工作正常,但它不会以我们需要的方式进入html表格格式

    <?php
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
     echo "<th>Firstname</th><th>Lastname</th><th>Email Address</th>";
     $arrayExplode = explode("\n", $array);
     $k = 0;
     foreach($array as $arrayOut)
     {
     $arrayExplodeTwo = explode(" ", $arrayExplode[$k]);
     $firstName =  $arrayExplodeTwo[0];
     $lastName = $arrayExplodeTwo[1];
     $email = $arrayExplodeTwo[2];
     echo '<tr><td>'; echo $firstName; echo '</td></tr>';echo '<tr><td>'; 
     echo $lastName; echo '</td></tr>';
     echo '<tr><td>'; echo $email; echo '</td></tr>';
     $k++;  
     }


     ?>
代码不希望正确地将数据打印到表中。在foreach循环中取出爆炸的特定分解,将其输出到表中。但是,这样做并不会将它们拆分为表中的空格,每行只有一个框,其余两个框为空。它将数据打印到表的Firstname列中,每行的数据。将数据格式化为特定行会导致它在列中不打印任何内容,只打印一个空表

下面是在将explode函数添加到靠近代码底部的foreach循环之前,我的代码是如何输出到web浏览器的

       Firstname                            Lastname    Email Address
       Anon emous anon@email.com
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Person Anon Anon@emailaddr.com has signed in.
       Person AnonyMouse AnonyMouse@emailaddr.com has signed in.
       Person Name randomaddr@example.com has signed in.
       Test Person thispersonisatest@fake.com has signed in.
       Test PersonTwo thispersonisatestTwo@fake.com has signed in.
       random name randomname@example.com
       test personand testPersonAnd@testemail.com has signed in.
在foreach循环中添加explode函数后,结果如下所示:

       Firstname    Lastname    Email Address
表格期间没有数据,作为空白表格输出


提前感谢您的帮助。

我对您的guestBook.txt文件做了一些假设,并得到了以下代码


vs


$firstName
$lastName
$email

希望这有帮助

几乎与@dchao5建议的解决方案相同。正如他提交的那样,他正在做这件事。主要的区别是使用php的内置模板语法,这样就不会在业务逻辑中混合html代码。首先进行处理,生成页面需要呈现的元素,然后使用模板语法在页面中呈现它们,这样做总是比较干净的。使维护更加清晰

<?php

    // all the same did not touch
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
    //--------------------------------------


     $formatted = array();
     foreach($array as $row)
     {

        if($row != ''){

            $columns = explode(" ", $row);

            $formatted[] = array(
                'first'     => $columns[0],
                'last'      => $columns[1],
                'email'     => $columns[2]
            );

        }

     }
?>

<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($formatted as $row): ?>
        <tr>
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td><?php echo $row['email']; ?>td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

名字
姓
电子邮件
td>

首先,注意到您有
”;echo$firstName;回显“”
。这是每行1个单元格,而不是每行3个单元格。它应该是
。第二,为什么它是
foreach($arrayOut作为数组)
而不是
foreach($arrayExplode作为$arrayOut)
第三,var\u dump($arrayExplodeTwo)的结果是什么?
<?php

# Read and parse the file
$fileInput = "guestBook.txt";
$fileInputInData = file_get_contents($fileInput);
$array=explode("\n", $fileInputInData);

# Sort the data
for($j = 0; $j < count($array); $j ++)
{
   for($i = 0; $i < count($array)-1; $i ++)
   {
      if($array[$i] > $array[$i+1])
      {
         $temp = $array[$i+1];
         $array[$i+1]=$array[$i];
         $array[$i]=$temp;
      }
   }
}

# Check the sort results
#var_dump($array);

# Echo the table header
echo "<tr><th>Firstname</th><th>Lastname</th><th>Email Address</th></tr>";

# Loop through each element of the sorted array
foreach($array as $arrayOut)
{
   # Explode the current array element by spaces
   $arrayExplode = explode(" ", $arrayOut);

   # Assign the data
   $firstName =  $arrayExplode[0];
   $lastName = $arrayExplode[1];
   $email = $arrayExplode[2];

   # Echo out the results
   echo '<tr>';
   echo ' <td>'; echo $firstName; echo '</td>';
   echo ' <td>'; echo $lastName; echo '</td>';
   echo ' <td>'; echo $email; echo '</td>';
   echo '</tr>';
}  
?>
<tr><td>$firstName</td></tr>
<tr><td>$lastName</td></tr>
<tr><td>$email</td></tr>
    <tr>
     <td>$firstName</td>
     <td>$lastName</td>
     <td>$email</td>
    <tr>
<?php

    // all the same did not touch
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("\n", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
    //--------------------------------------


     $formatted = array();
     foreach($array as $row)
     {

        if($row != ''){

            $columns = explode(" ", $row);

            $formatted[] = array(
                'first'     => $columns[0],
                'last'      => $columns[1],
                'email'     => $columns[2]
            );

        }

     }
?>

<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($formatted as $row): ?>
        <tr>
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td><?php echo $row['email']; ?>td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>