使用PHP在HTML表中显示数组中的数据

时间:2018-10-15 20:18:13

标签: php

我正在尝试如下显示来自表中数组的数据:

Download    Upload    BT            TTB
10          5         Available     Available
9           3         Available     Not Available

数组如下:

[products] => Array
    (
        [0] => stdClass Object
            (
                [name] => 20CN ADSL Max
                [likely_down_speed] => 1
                [likely_up_speed] => 0.1
                [availability] => 1
                [availability_date] => 
                [speed_range] => 0.75 to 2.5
                [provider] => WBC_20CN
                [technology] => ADSL
                [limited_capacity] => 
            )

        [1] => stdClass Object
            (
                [name] => 21CN ADSL 2+
                [likely_down_speed] => 1
                [likely_up_speed] => 0.05
                [availability] => 1
                [availability_date] => 
                [speed_range] => 1 to 3.5
                [provider] => WBC_21CN
                [technology] => ADSL2+
                [limited_capacity] => 
            )

        [2] => stdClass Object
            (
                [name] => 21CN Annex M
                [likely_down_speed] => 1
                [likely_up_speed] => 0.5
                [availability] => 1
                [availability_date] => 
                [speed_range] => 1 to 3.5
                [provider] => WBC_21CN
                [technology] => AnnexM
                [limited_capacity] => 
            )

        [3] => stdClass Object
            (
                [name] => Fibre to the Cabinet (FTTC)
                [likely_down_speed] => 
                [likely_up_speed] => 
                [availability] => 
                [availability_date] => 
                [speed_range] => 
                [provider] => WBC_21CN
                [technology] => FTTC
                [limited_capacity] => 
            )

        [4] => stdClass Object
            (
                [name] => Fibre to the Premises (FTTP)
                [likely_down_speed] => 
                [likely_up_speed] => 
                [availability] => 
                [availability_date] => 
                [speed_range] => 
                [provider] => WBC_21CN
                [technology] => FTTP
                [limited_capacity] => 
            )

        [5] => stdClass Object
            (
                [name] => FTTP on Demand
                [likely_down_speed] => 330
                [likely_up_speed] => 30
                [availability] => 1
                [availability_date] => 
                [speed_range] => 
                [provider] => WBC_21CN
                [technology] => FTTPoD
                [limited_capacity] => 
            )

        [6] => stdClass Object
            (
                [name] => G.Fast
                [likely_down_speed] => 
                [likely_up_speed] => 
                [availability] => 
                [availability_date] => 
                [speed_range] => 
                [provider] => WBC_21CN
                [technology] => G.Fast
                [limited_capacity] => 
            )

        [7] => stdClass Object
            (
                [name] => TalkTalk LLU ADSL 2+
                [likely_down_speed] => 0.89
                [likely_up_speed] => 
                [availability] => 1
                [availability_date] => 
                [speed_range] => 0.06 to 1.73
                [provider] => TTB
                [technology] => LLU
                [limited_capacity] => 
            )

        [8] => stdClass Object
            (
                [name] => TalkTalk LLU Annex M
                [likely_down_speed] => 0.39
                [likely_up_speed] => 0.5
                [availability] => 1
                [availability_date] => 
                [speed_range] => 1.13 to 2.62
                [provider] => TTB
                [technology] => AnnexM
                [limited_capacity] => 
            )

        [9] => stdClass Object
            (
                [name] => TalkTalk FTTC
                [likely_down_speed] => 
                [likely_up_speed] => 
                [availability] => 
                [availability_date] => 
                [speed_range] => 
                [provider] => TTB
                [technology] => FTTC
                [limited_capacity] => 
            )

    )

由于数组是从API返回的,因此我无法更改其格式。

末端列BT和TTB基于数组中的provider键。

由于它们显示为产品,因此我无法确定如何在上面的表中显示它们。

我考虑过要做这样的事情:

foreach($availability->products as $res) {
    if($res->provider == 'TTB') {
        $ttb[] = $res;
    } else {
        $bt[] = $res;
    }
}

3 个答案:

答案 0 :(得分:1)

我将做一些假设,显示代码,然后解释:

  • providerTTB表示TTB可用,而BT不可用,并且取反。
  • 数据结构是一个具有名为product的键的对象,它是由许多对象组成的数组
  • 下载/上传来自以likely_开头的字段
  • 当您说表输出时,我假设您是指HTML表

考虑到这一点,请查看以下代码:

$result = new stdClass;
$result->products = [
  (object) [
    'name' => '20CN ADSL Max',
    'likely_down_speed' => 1,
    'likely_up_speed' => 0.1,
    'availability' => 1,
    'availability_date' => null,
    'speed_range' => '0.75 to 2.5',
    'provider' => 'WBC_20CN',
    'technology' => 'ADSL',
    'limited_capacity' => null
  ],

  (object) [
    'name' => 'TalkTalk FTTC',
    'likely_down_speed' => null,
    'likely_up_speed' => null,
    'availability' => null,
    'availability_date' => null,
    'speed_range' => null,
    'provider' => 'TBB',
    'technology' => 'FTTC',
    'limited_capacity' => null
  ],
];

echo "<table><tr>";
echo "<th>Download</th><th>Upload</th><th>BT</th><th>TBB</th>";
echo "</tr>";

foreach ($result->products as $product) {
  echo "<tr>";
  echo "<td>" . (int) $product->likely_down_speed . "</td>";
  echo "<td>" . (int) $product->likely_up_speed . "</td>";
  echo "<td>" . ($product->provider === 'TTB' ? "Not Available" : "Available") . "</td>";
  echo "<td>" . ($product->provider !== 'TTB' ? "Available" : "Not Available") . "</td>";
  echo "</tr>";
}

echo "</table>";

这是怎么回事。

我认为您的数据结构是第一个变量$result。当有空白输出时,我将它们设置为空。

然后,我们使用echo语句创建一个表,以提供所需的标题。

接下来,循环浏览产品。为每种产品排一行。

将第一个下/上输出为整数。 请注意,这将使空值输出为零。您可能要更改它。

接下来,根据我们的提供商,我们将打印可用或不可用。因此,如果提供商是TTB,则意味着我们将打印BT不可用,而TTB可用。

此表结束。

答案 1 :(得分:0)

在您的html(查看)文件中

<table>
<thead>
<tr>
 <td>Download</td>
 <td>Upload</td>
 <td>BT</td>
 <td>TTB</td>
</tr>

</thead>

<tbody>
<?php
  foreach($availability["products"] as $res)
 {
  ?>
   <tr>
     <td><?php  echo $res->likely_down_speed;  ?></td>
     <td><?php  echo $res->likely_up_speed;  ?></td>
     <td><?php  echo $res->provider !="TTB"?"Available":"Not Available";  ?></td>
     <td><?php  echo $res->provider =="TTB"?"Available":"Not Available";  ?></td>
   </tr>
  <?php
 }


?>


</tbody>



</table>

其中$ res代表$ availability [“ products”]数组中的每个stdClass对象

答案 2 :(得分:0)

这是简单的第一个父数组      $ availability ['产品']; 喜欢:

array( // this line is first parent array
array('key' => 'value','key' => 'value'), // this line is objects
array('key' => 'value','key' => 'value'), // this line is objects
array('key' => 'value','key' => 'value'), // this line is objects
array('key' => 'value','key' => 'value'), // this line is objects
);


foreach($availability['products'] as $res) {
        if($res->provider == 'TTB') {
            $ttb[] = $res;
        } else {
            $bt[] = $res;
        }
    }

您的输出:

 print_r($ttb);
    print_r($bt);