从PHP数组创建HTML表

时间:2019-06-24 04:43:09

标签: php html arrays

这就是我从$_POST定义数组的方式

$destination = $_POST['destination'];
$depart_date = $_POST['depart_date'];

print_r结果如下:

Array
(
    [destination] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [depart_date] => Array
        (
            [0] => 2019-06-04
            [1] => 2019-06-06
            [2] => 2019-06-13
            [3] => 2019-06-22
        )
)

现在,我想将这些数组回显到HTML表中。

这是我尝试的方式:

if (is_array($destination)) {       
    $dtble = "<table>
                <tr>
                  <th>Destination</th>
                  <th>Depart Date</th>
                </tr>";
    foreach($destination as $k => $v){
      $d = $depart_date[$k];
      $dtble .= "<tr>
                   <td>{$v}</td>
                   <td>{$d}</td>
                 <tr>";
    }
    $dtble .= "</table>";
}

但是它给出的输出是这样的:

Destination     Depart Date
a               2019-06-04
b               2019-06-06
c               2019-06-13
d               2019-06-22
                2019-06-04

**新更新** 这就是我定义这两个数组及其输出的方式:

$destination = array_values(array_unique($_POST['destination'])); 
if (is_array($destination)) {
    $destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING); 
} 

echo '<pre>',print_r($destination).'</pre>';

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => 
)
1

// Depart Date
if (!empty($_POST['depart_date'])) { 
    $depart_date = array_values(array_unique($_POST['depart_date']));// to remove duplicate and re-index array
    if (is_array($depart_date)) {
        foreach($depart_date as $dt){
          if (preg_match("/\d{4}\-\d{2}-\d{2}/", $dt)) {  
            $depart_date[] = $dt;
          } else{
                array_push($errors, '- Depart date is NOT in required format!');
          }
        }
    } 
}

echo '<pre>',print_r($depart_date).'</pre>';

Array
(
    [0] => 2019-06-03
    [1] => 2019-06-04
    [2] => 2019-06-05
    [3] => 2019-06-06
    [4] => 2019-06-03
    [5] => 2019-06-04
    [6] => 2019-06-05
    [7] => 2019-06-06
)

您可以看到日期2019-06-04正在重复。 谁能说出如何解决这个问题。

3 个答案:

答案 0 :(得分:2)

您显示的代码似乎工作正常。

我已经对此进行了测试,并且可以正常工作。

$_POST = array(

  'destination' => array(

    '0' => 'a',
    '1' => 'b',
    '2' => 'c',
    '3' => 'd'

    ),

  'depart_date' => array(

    '0' => '2019-06-04',
    '1' => '2019-06-06',
    '2' => '2019-06-13',
    '3' => '2019-06-22'

    )

);

$destination  = $_POST['destination'];
if (is_array($destination)) {
    $destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING); //<---This is your problem.  You are adding an extra element to the array.
    //print_r($destination);
}

if (!empty($_POST['depart_date'])) {
    $depart_date = $_POST['depart_date'];
    if (is_array($depart_date)) {
        foreach($depart_date as $dt){
          if (preg_match("/\d{4}\-\d{2}-\d{2}/", $dt)) {
            $depart_date[] = $dt;
          } else{
                array_push($errors, '- Depart date is NOT in required format!');
          }
        }
    }
}

if (is_array($destination)) {
    $dtble = "<table>
                <tr>
                  <th>Destination</th>
                  <th>Depart Date</th>
                </tr>";
    foreach($destination as $k => $v){
      $d = $depart_date[$k];
      $dtble .= "<tr>
                   <td>{$v}</td>
                   <td>{$d}</td>
                 </tr>";
    }
    $dtble .= "</table>";
}

echo $dtble;

更新:

您的问题在这里:

在您要消毒的$destination数组中,您正在使用此行向数组添加另一个元素:

$destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING);

当您遍历数组时,我将遍历数组并清理每个值。

如果我删除代码行,您的代码就可以正常工作。

这将返回:

Destination     Depart Date
a               2019-06-04
b               2019-06-06
c               2019-06-13
d               2019-06-22

filter_input不能用于数组,只能用于单个值。建议您阅读filter_input_array的文档,并调整代码以适应要求。

答案 1 :(得分:1)

您可以使用array_combine()将它们组合成一个关联数组。然后,您可以轻松地遍历它们。确保每个目的地都有出发日期,反之亦然

<?php

   $destination = $_POST['destination'];
   $depart_date = $_POST['depart_date'];
   echo '<table>';
   echo '<tr>';
   echo '<th>Destination</th>';
   echo '<th>Depart Date</th>';
   echo '</tr>';
   foreach(array_combine($destination,$depart_date) as 
   $destination=>$depart_date)
   {
      echo "<tr>";
      echo "<td>$destination</td>";
      echo "<td>$depart_date</td>"
      echo "</tr>";
    }
    echo "</table>";

答案 2 :(得分:1)

您可以将array_combineforeach一起使用

$arr = array_combine($destination, $depart_date);
$html =  "<table>
            <tr>
              <th>Destination</th>
              <th>Depart Date</th>
            </tr>";
foreach ($arr as $key => $value) {
    $html .= "<tr>
            <td>{$key}</td>
            <td>{$value}</td>
        </tr>";
}
$html .= "</table>";
echo $html;