如何在PHP中使用多维数组创建表

时间:2019-06-22 13:26:24

标签: php

实际上这是我来自API的真实数据。

$main2 = array(
    'sup1' => array(
        '01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
        '04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
    ),
    'sup2' => array(
        '01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
        '04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
    ),
);

我们需要使用此数组的相同输出。

我们在表头中创建表中的所有日期,例如在表头中显示的第一个关键日期。 -然后在第一行中显示数据供应商sup1  第二行中的sup2

像下面一样放

|Supplier Name  |01-Jun-2019-TO-03-Jun-2019 |04-Jun-2019-TO-08-Jun-2019|<br/>
|sup1|54|TATA|10|Rel|
<br/>
|sup2|54|TCS|55|JIO|

1 个答案:

答案 0 :(得分:0)

在这里您的代码已修改:

<?php
$main2 = array(
    '01-Jun-2019-TO-03-Jun-2019' => array(
        'sup1' => array("connection_count" => 54, "source_type" => 'TATA'),
        'sup2' => array("connection_count" => 10, "source_type" => 'Rel'),
    ),
    '04-Jun-2019-TO-08-Jun-2019' => array(
        'sup1' => array("connection_count" => 54, "source_type" => 'TCS'),
        'sup2' => array("connection_count" => 55, "source_type" => 'Jio'),
    ),
);
$suplist = array('sup1', 'sup2');

echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";

foreach ($main2 as $key => $value) {
    echo "<td colspan=2>" . $key . "</td>";
}

echo "</tr>";

foreach ($suplist as $supplier) {
    echo "<tr><td>$supplier</td>";
    foreach ($main2 as $value) {
        echo "<td>" . $value[$supplier]['connection_count'] . "</td>";
        echo "<td>" . $value[$supplier]['source_type'] . "</td>";
    }
    echo "</tr>";
}

echo "</table>";

这是示例输出(为清晰起见,对其进行了重新格式化):

<table border=1>
  <tr>
    <td>Supplier Name</td>
    <td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
    <td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
  </tr>
  <tr>
    <td>sup1</td>
    <td>54</td>
    <td>TATA</td>
    <td>54</td>
    <td>TCS</td>
  </tr>
  <tr>
    <td>sup2</td>
    <td>10</td>
    <td>Rel</td>
    <td>55</td>
    <td>Jio</td>
  </tr>
</table>

这是您的代码,可用于更新的输入数据格式:

$main2 = array(
    'sup1' => array(
        '01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
        '04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
    ),
    'sup2' => array(
        '01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
        '04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
    ),
);

$suppliers = array_keys($main2);

$dateRanges = [];
foreach ($main2 as $supplierData) {
    $dateRanges = array_merge($dateRanges, array_keys($supplierData));
}
$dateRanges = array_values(array_unique($dateRanges));
sort($dateRanges);

echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";

foreach ($dateRanges as $dateRange) {
    echo "<td colspan=2>" . $dateRange . "</td>";
}

echo "</tr>";

foreach ($suppliers as $supplier) {
    echo "<tr><td>$supplier</td>";
    foreach ($dateRanges as $dateRange) {
        if (isset($main2[$supplier][$dateRange])) {
            echo "<td>" . $main2[$supplier][$dateRange]['connection_count'] . "</td>";
            echo "<td>" . $main2[$supplier][$dateRange]['source_type'] . "</td>";
        } else {
            echo "<td></td><td></td>";
        }
    }
    echo "</tr>";
}

echo "</table>";

这是示例输出(为清晰起见而设置):

<table border=1>
  <tr>
    <td>Supplier Name</td>
    <td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
    <td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
  </tr>
  <tr>
    <td>sup1</td>
    <td>54</td>
    <td>TATA</td>
    <td>10</td>
    <td>Rel</td>
  </tr>
  <tr>
    <td>sup2</td>
    <td>54</td>
    <td>TCS</td>
    <td>55</td>
    <td>Jio</td>
  </tr>
</table>