如何在每个表列中查找最小值并在 php 中显示一个类


How to find the minimum value in each table column and display a class in php

嗨,我使用 YQL 从外部网站拉了一个表。该表包含时间列表,例如: 但是列数可以更改为包含更多列。

| Name   | T1     | T2     | T3     |
|--------|--------|--------|--------|
| Name 1 | 23.234 | 45.234 | 16.456 |
| Name 2 | 23.389 | 44.322 | 15.222 |
| Name 5 | 22.890 | 44.221 | 15.345 |

我正在尝试做的是从每列中获取最低值。我已经能够通过使用这个函数来获得它。

<?php 
   $sectim = [];
   for ($st=1; $st < count($allRows); $st++) { 
       $sectim[] = $phpObj->query->results->tbody->tr[1]->td[6]->content;
   }
   echo "<pre>"; print_r($sectim); echo "</pre>";  
   echo "<pre>"; print_r(min($sectim)); echo "</pre>"; 
 ?>

这为我提供了该列中所有时间的列表,并给了我最少的时间。我正在努力解决的是如何以一种可以从每个表列中获得最小值的方式做到这一点,因为列数不是固定的,可以是 3 列 4 甚至 9 列。我正在考虑使用另一个 for 语句来遍历所有列,但我不知道这是否有效。

您可以使用

此查询

select min(T1) as column1,min(T2) as column2,min(T3) as column3 from tabel

这将为您提供每列的最小值。

如果您正在寻找构建动态查询:

$array = array(
   'column1' => 'T1',
   'column2' => 'T2',
   'column3' => 'T3'
);
$column_binder = array();
foreach ($array as $key=>$value){
    $column_binder[] = " min($value) as $key ";
}
$query = "SELECT ".impode(",", $column_binder)." From tabel_name";