0

I have the following array:

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
            )

    )

I am trying to create a loop which will print array data in separate tables, on base on the key SUPPLIER_C

So one table will contain data where the key SUPPLIER_C is 000620, another table for the data where the key SUPPLIER_C is 003053 etc...

My code bellow, does not produce the results that I want:

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

The code above print a table header and only the first row, and then print again new table, even the key SUPPLIER_C is not changed.

Any help will be deeply appreciated.

3
  • 2
    Split the array to some ones having the only key. and build tables for each array Commented Oct 31, 2017 at 7:16
  • Any idea how that can be done? Can you post any code snippet? Commented Oct 31, 2017 at 7:18
  • Iterate over array and check required key. Add current item to another array defined by the key. Commented Oct 31, 2017 at 7:19

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.