php将数组行重新排列成列,以显示在表

时间:2016-04-11 14:22:44

标签: php mysql arrays

我的数组由我的数据库返回。

Array
(
[0] => Array
    (
        [dwg_id] => 1
        [sws_dwg_no] => 160-001
        [client_dwg_no] => KSB/9972475771/WP5/02/001
        [dwg_title] => OVERALL LAYOUT AND REFERENCE DRAWING
        [dwg_by] => JES
        [dwg_date] => 2016-03-11 16:13:00
        [dwg_rev] => A
    )

[1] => Array
    (
        [dwg_id] => 1
        [sws_dwg_no] => 160-001
        [client_dwg_no] => KSB/9972475771/WP5/02/001
        [dwg_title] => OVERALL LAYOUT AND REFERENCE DRAWING
        [dwg_by] => JES
        [dwg_date] => 2016-04-04 21:41:31
        [dwg_rev] => B
    )

[2] => Array
    (
        [dwg_id] => 2
        [sws_dwg_no] => 160-002
        [client_dwg_no] => KSB/9972475771/WP5/02/002
        [dwg_title] => OVERALL LAYOUT AND REFERENCE DRAWING
        [dwg_by] => JES
        [dwg_date] => 2016-03-11 16:13:00
        [dwg_rev] => A
    )

[3] => Array
    (
        [dwg_id] => 2
        [sws_dwg_no] => 160-002
        [client_dwg_no] => KSB/9972475771/WP5/02/002
        [dwg_title] => OVERALL LAYOUT AND REFERENCE DRAWING
        [dwg_by] => JES
        [dwg_date] => 2016-04-04 21:41:31
        [dwg_rev] => B
    )

[4] => Array
    (
        [dwg_id] => 3
        [sws_dwg_no] => 160-100
        [client_dwg_no] => KSB/9972475771/WP5/02/100
        [dwg_title] => CONCRETE WORK PLAN AND DETAIL
        [dwg_by] => JES
        [dwg_date] => 2016-03-11 16:13:00
        [dwg_rev] => A
    )

我希望按此顺序排列数组,这样我就可以使用新数组填充html表。新数组的[0]行是表的标题。

这就是新数组应该是什么样子

Array
(
[0] => Array
    (
        [0] => Drawing no.
        [1] => 160-001
        [2] => 160-002
        [3] => 160-100
        [4] => 160-101
    )

[1] => Array
    (
        [0] => Client drawing no.
        [1] => KSB/9972475771/WP5/02/001
        [2] => KSB/9972475771/WP5/02/002
        [3] => KSB/9972475771/WP5/02/100
        [4] => KSB/9972475771/WP5/02/101
    )

[2] => Array
    (
        [0] => Title
        [1] => OVERALL LAYOUT AND REFERENCE DRAWING
        [2] => OVERALL LAYOUT AND REFERENCE DRAWING
        [3] => CONCRETE WORK PLAN AND DETAIL
        [4] => CONCRETE WORK SECTION
    )

[3] => Array
    (
        [0] => By
        [1] => JES
        [2] => JES
        [3] => JES
        [4] => JES
    )

[4] => Array
    (
        [0] => A
        [1] => 2016/04/04
        [2] => 2016/04/04
        [3] => 2016/03/11
        [4] => 2016/03/11
    )

[5] => Array
    (
        [0] => B
        [1] => 2016/04/04
        [2] => 2016/04/04
    )

   )

这是我的代码,它适用于修订日期。 $ result是db的输入数组。

$new_array = array();
    if(count($result) > 0)
    {
        $dwg_no = array(0=>'Drawing no.') + array_column($result, 'sws_dwg_no', 'dwg_id');
        $client_dwg_no =array(0=> 'Client drawing no.') + array_column($result, 'client_dwg_no', 'dwg_id');
        $title = array(0=>'Title') + array_column($result,'dwg_title', 'dwg_id');
        $by = array(0=>'By') + array_column($result, 'dwg_by', 'dwg_id');       
        foreach($result as $row){
            $rev = $row['dwg_rev'];
            if($rev == $row['dwg_rev']){
                $rev_date[$row['dwg_id']] = date('Y/m/d', strtotime($row['dwg_date']));
            }
            elseif($rev >= $row['dwg_rev']) {
                $rev_date[$row['dwg_id']] = '';
            }

            ${'rev_'. $rev . '_date'} = array(0 => $rev) + $rev_date;
        }
        $new_array = array(
                        0 => $dwg_no,
                        1 => $client_dwg_no,
                        2 => $title,
                        3 => $by,
                        4 => ${'rev_'. $rev . '_date'});

    }

所以问题是

  1. 将修订日期安排在正确的修订版本下。我的代码目前还没有这样做。根据您刷新页面的次数,日期会发生变化。
  2. $ new_array必须是动态的,以便修订的数量不受限制。修订顺序来自A - Z,然后是1 - 99
  3. 表格应如下所示 enter image description here

1 个答案:

答案 0 :(得分:0)

只需进行矩阵转置

function transpose($array) {
   array_unshift($array, null);
   call_user_func_array('array_map', $array);
}

在:

160-001 | 160-002 | 160-003 | 
KSB/9972475771/WP5/02/001 | KSB/9972475771/WP5/02/002 | KSB/9972475771/WP5/02/100 | 

后:

160-001 | KSB/9972475771/WP5/02/001 | 
160-002 | KSB/9972475771/WP5/02/002 | 
160-003 | KSB/9972475771/WP5/02/100 | 

这是我的完整代码:

<?php

$data = [
    ['160-001','160-002','160-003'],
    ['KSB/9972475771/WP5/02/001', 'KSB/9972475771/WP5/02/002 ', 'KSB/9972475771/WP5/02/100']
];

function transpose($array) {
        array_unshift($array, null);
        return call_user_func_array('array_map', $array);
}

foreach($data as $row) {
    foreach($row as $column) {
            echo $column . " | ";
    }
        echo "<br>";
}

$data = transpose($data);

foreach($data as $row) {
    foreach($row as $column) {
            echo $column . " | ";
    }
        echo "<br>";
}

?>