基于数组动态构建数组

时间:2019-07-09 09:42:52

标签: php html arrays

除了数组中的列数据与列标题不匹配之外,我想基于数组构建数组。 我该如何将正确的数据带给正确的collone所有者?

当前我得到此表,除了项目03和items04的数据不应显示在R2018_04列中,而应显示在R2020_03列中

Kpi      | R2018_04 | R2019_B2 | R2020_03 | No Data |
---------|----------|----------|----------|---------|
Items01  | 0        | 0        |          |         | 
Items02  | 11       |          |          |         |  
Items03  | 4        |          |          |         |  
Items04  | 4        |          |          |         |     
Items05  | -        | -        | -        | N/A     |   
Items06  | -        | -        | -        | N/A     |   
Items07  | -        | -        | -        | N/A     |  
Items08  | -        | -        | -        | N/A     |     
Items09  | -        | -        | -        | N/A     |       
Items010 | -        | -        | -        | N/A     |     

这里是使用的数组,存储在$ tab2变量中

$tab2 = [
  "head" => [
    "R2018_04" => "R2018_04",
    "R2019_B2" => "R2019_B2",
    "R2020_03" => "R2020_03",
    "No Data" => "No Data",
  ],
  "Item01" => [
    "R2018_04" => [
      "response" => true,
      "value" => 0,
    ],
    "R2019_B2" => [
      "response" => true,
      "value" => 0,
    ],
  ],
  "Item02" => [
    "R2020_03" => [
      "response" => false,
      "value" => 11,
    ],
  ],
  "Item03" => [
    "R2020_03" => [
      "response" => false,
      "value" => 4,
    ],
  ],
  "Item04" => [
    "R2020_03" => [
      "response" => false,
      "value" => 4,
    ],
  ],
  "Item05" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
  "Item06" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
  "Item07" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
  "Item08" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
  "Item09" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
  "Item10" => [
    "No Data" => [
      "response" => 2,
      "value" => 0,
    ],
  ],
];

这是我用来得出错误表格结果的代码

// count number of head 
        $nbHead = count($tab2['head'])-1;

        // build the table
        $output = '<table style="border: 1px solid #333; width:100%; font-family:calibri, Arial, Helvetica, sans-serif;">';
        $output .= '<thead><tr>';
        $output .= '<th style="text-align:center; width: 30%; border-bottom: 1px solid #333;">Kpi</th>';
        foreach ($tab2['head'] as $value) {
            $output .= '<th style="text-align:left; border-bottom: 1px solid #333;">'.$value.'</th>';
        }
        $output .= '</tr></thead> ';

        foreach ($tab2 as $key => $value) {

            if($key != 'head'){

                $output .= '<tr>';
                $output .= '<td style="text-align:left;">'.$key.'</td>';

                foreach ($value as $k => $v) {
                    // get status with css
                    switch ($v['response']) {
                        case 1:
                            $status = 'Pass';
                            $kpi_value = $v['value'];
                            $style = "green";
                        break;
                        case 0:
                            $status = 'Fail';
                            $kpi_value = $v['value'];
                            $style = "red";
                        break;
                        case 2:
                            $status = 'N/A';
                            $style = "orange";
                        break;
                        default:
                            $status = 'N/A';
                        break;
                    }

                    if($status == 'N/A'){
                        for ($i=0; $i < $nbHead ; $i++) { 
                            $output .= '<td style="text-align:center;"> - </td>';
                        }
                        $output .= '<td style="text-align:center; background-color:'.$style.'">'.$status.'</td>';
                    }else{
                        $output .= '<td style="text-align:center; color: white; background-color:'.$style.';">'.$kpi_value.'</td>';
                    }
                }
                $output .= '</tr>';
            }
        }
        $output .= '</table>';
        //exit();
        return $output;

数组的数据是动态生成的,因此数组的列可以根据数组报告的数据而变化。 这是具有给定数组的预期数组。

Kpi      | R2018_04 | R2019_B2 | R2020_03 | No Data |
---------|----------|----------|----------|---------|
Items01  | 0        | 0        |          |         | 
Items02  | 11       |          |          |         |  
Items03  |          |          |  4       |         |  
Items04  |          |          |  4       |         |     
Items05  | -        | -        | -        | N/A     |   
Items06  | -        | -        | -        | N/A     |   
Items07  | -        | -        | -        | N/A     |  
Items08  | -        | -        | -        | N/A     |     
Items09  | -        | -        | -        | N/A     |       
Items010 | -        | -        | -        | N/A     |

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,我认为您当前想要的结果是错误的,Items02的11位置似乎没有什么意义。假设它实际上属于R2020_03列,这就是我的处理方法。

数据的最里面的循环被替换为头文件$tab2['head']的循环。这样,无论当前项实际包含多少个条目,生成正确的表单元格的 amount 都将没有问题。然后,将使用$tab2['head'] key 来检查该项是否具有相应的元素集,并相应地创建输出。该解决方案还假设,如果存在No Data条目,则这也意味着它将是该项目的第一个条目,并且是 only 条目。

// build the table
$output = '<table style="border: 1px solid #333; width:100%; font-family:calibri, Arial, Helvetica, sans-serif;">';
$output .= '<thead><tr>';
$output .= '<th style="text-align:center; width: 30%; border-bottom: 1px solid #333;">Kpi</th>';
foreach ($tab2['head'] as $value) {
    $output .= '<th style="text-align:left; border-bottom: 1px solid #333;">'.$value.'</th>';
}
$output .= '</tr></thead> ';

foreach ($tab2 as $key => $value) {

    if($key != 'head'){

        $output .= '<tr>';
        $output .= '<td style="text-align:left;">'.$key.'</td>';

        foreach($tab2['head'] as $colKey => $colValue) {
            if(isset($value['No Data'])) {
                if($colKey != 'No Data') {
                    $output .= '<td style="text-align:center;"> - </td>';
                }
                else {
                    $output .= '<td style="text-align:center;"> N/A </td>';
                }
            }
            else {
                $style = 'transparent'; // defaults for
                $kpi_value = '';        // “empty” cells
                if(isset($value[$colKey])) {
                    switch ($value[$colKey]['response']) {
                        case 1:
                            $kpi_value = $value[$colKey]['value'];
                            $style = "green";
                        break;
                        case 0:
                            $kpi_value = $value[$colKey]['value'];
                            $style = "red";
                        break;
                        case 2:
                            $style = "orange";
                        break;
                    }
                }
                $output .= '<td style="text-align:center; color: white; background-color:'.$style.';">'.$kpi_value.'</td>';
            }
        }
        $output .= '</tr>';
    }
}
$output .= '</table>';

这将为您提供下表,如该小提琴所示:https://jsfiddle.net/6L5weubj/


在这里我省略了您的通过/失败状态,因为正如我评论的那样,从您的示例中尚不清楚应该在何时何地影响或显示结果。如果该状态对您很重要,并且还需要在表中输出或以某种方式影响样式,请尝试自己再次添加。