为特定键中的每个不同数组值创建一个表

时间:2017-10-31 07:10:11

标签: php arrays foreach

我有以下数组:

Array
(
[data] => Array
    (
        [0] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-01-29T22:00:00.0000
                [INV_NUM] => AGG-81
                [SERV_CODE] => 0845
            )

        [1] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-02-21T22:00:00.0000
                [INV_NUM] => AGG-82
                [SERV_CODE] => 0845
            )

        [2] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-03-27T21:00:00.0000
                [INV_NUM] => AGG-83
                [SERV_CODE] => 0845
            )

        [3] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-04-23T21:00:00.0000
                [INV_NUM] => 65
                [SERV_CODE] => 0845
            )

        [4] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-05-25T21:00:00.0000
                [INV_NUM] => 66
                [SERV_CODE] => 0845
            )

        [5] => Array
            (
                [SUPPLIER_C] => 000620
                [SUPPLIER_NAME] => ASTRA LTD
                [DATE] => 2017-06-25T21:00:00.0000
                [INV_NUM] => 67
                [SERV_CODE] => 0845
            )


        [6] => Array
            (
                [SUPPLIER_C] => 003053
                [SUPPLIER_NAME] => ECOMM TRADE
                [DATE] => 2017-01-31T22:00:00.0000
                [INV_NUM] => 44
                [SERV_CODE] => 0856
            )

        [7] => Array
            (
                [SUPPLIER_C] => 003053
                [SUPPLIER_NAME] => ECOMM TRADE
                [DATE] => 2017-02-27T22:00:00.0000
                [INV_NUM] => 47
                [SERV_CODE] => 0856
            )

        [8] => Array
            (
                [SUPPLIER_C] => 003053
                [SUPPLIER_NAME] => ECOMM TRADE
                [DATE] => 2017-03-30T21:00:00.0000
                [INV_NUM] => 7
                [SERV_CODE] => 0856
            )

        [9] => Array
            (
                [SUPPLIER_C] => 003053
                [SUPPLIER_NAME] => ECOMM TRADE
                [DATE] => 2017-04-29T21:00:00.0000
                [INV_NUM] => 20
                [SERV_CODE] => 0856
            )


        [10] => Array
            (
                [SUPPLIER_C] => 004146
                [SUPPLIER_NAME] => ACTADVICE MANAGEMENT LTD
                [DATE] => 2017-05-27T21:00:00.0000
                [INV_NUM] => 14
                [SERV_CODE] => 9916
            )

        [11] => Array
            (
                [SUPPLIER_C] => 004146
                [SUPPLIER_NAME] => ACTADVICE MANAGEMENT LTD
                [DATE] => 2017-06-25T21:00:00.0000
                [INV_NUM] => 16
                [SERV_CODE] => 9916
            )

        [12] => Array
            (
                [SUPPLIER_C] => 004146
                [SUPPLIER_NAME] => ACTADVICE MANAGEMENT LTD
                [DATE] => 2017-06-25T21:00:00.0000
                [INV_NUM] => 17
                [SERV_CODE] => 9916
            )

    )

我正在尝试创建一个循环,它将根据键 SUPPLIER_C

在单独的表中打印数组数据

因此,一个表将包含键 SUPPLIER_C 为000620的数据,另一个表为 SUPPLIER_C 键为003053等的数据表...

我的代码如下,不会产生我想要的结果:

foreach ($suppliers_invoices['data'] as $key=>$invoice) {
.....table code here...
}

上面的代码打印一个表格标题,只打印第一行,然后重新打印新表,甚至键 SUPPLIER_C 也不会更改。

任何帮助都将深受赞赏。

1 个答案:

答案 0 :(得分:1)

$invoice_by_supplier = [];
$suppliers = [];

foreach ($suppliers_invoices['data'] as $invoice) {
  $supplier_c = $invoice['SUPPLIER_C'];
  $suppliers[$supplier_c] = $invoice['SUPPLIER_NAME'];
  $invoice_by_supplier[$supplier_c][] = $invoice;
}

foreach ($suppliers as $supplier_c => $supplier_name) {
  ...table head

  foreach ($invoice_by_supplier[$supplier_c] as $invoice) {
    .....table body here...
  }

  .. table foot
}