5

I have an array to be display in table format.

My table should display like this

Dept: Dept 1
No     Name    BankCode      Amount
1.     Name4   656789        119.20

Dept: Dept 2
No     Name    BankCode      Amount
1.     Name 1  DREW1CF       2775.24
2.     Name 2  DREW1CF       907.28
3.     Name 3  EWDR1CF       318.60

And this is my array

array:38 [▼
  0 => {#389 ▼
    +"FullName": "Name 1"
    +"BankCode": "DREW1CF"
    +"EntityName": "Dept 2"
    +"amount": "2775.24"
  }
  1 => {#391 ▼
    +"FullName": "Name 2"
    +"BankCode": "DREW1CF"
    +"EntityName": "Dept 2"
    +"amount": "907.28"
  }
  2 => {#392 ▼
    +"FullName": "Name 3"
    +"BankCode": "EWDR1CF"
    +"EntityName": "Dept 2"
    +"amount": "318.60"
  }
  3 => {#393 ▼
    +"FullName": "Name 4"
    +"BankCode": "656789"
    +"EntityName": "Dept 1"
    +"amount": "119.20"
  }
  4 => {#394 ▶}
  5 => {#395 ▶}
  .....and so on
]

The code in Laravel framework.

Currently i am stuck with my foreach.

If i am doing like below, it will not display like i need.

How can i implement the foreach to be what i need?

I not very good in foreach especially with $key => $value.

@php
$i = 1;
@endphp
@foreach ($getClaim as $claims)
<tr>
  <td>@php
      echo $i++
      @endphp</td>
  <td>{{$claims->FullName}}</td>
  <td>{{$claims->BankCode}}</td>
  <td>{{number_format($claims->amount, 2, '.', ',')}}</td>
</tr>
@endforeach
Umar Abdullah
  • 1,282
  • 1
  • 19
  • 37
shhrzl
  • 282
  • 1
  • 4
  • 15
  • How you detect the `Dept: Dept 1` section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table? – Nasser Ali Karimi Jan 03 '19 at 04:54

1 Answers1

2

Group the data before creating the table:

$groupedClaims = array_reduce($claims, function ($groups, $item) {
    if ($group = data_get($groups, $item->EntityName)) {
        // Using the spread operator
        data_set($groups, $item->EntityName, [ $item, ...$group ]);
        // Using array merge
        data_set($groups, $item->EntityName, array_merge($group, [$item]));
    } else {
        data_set($groups, $item->EntityName, [ $item ]);
    }
    return $groups;
}, []);

Then in your view file:

@foreach ($groupedClaims as $dept => $claims)
    <tr>
        <td colspan="4">Dept: {{ $dept }}</td>
    </tr>
    <tr>
        <td>No</td>
        <td>Name</td>
        <td>BankCode</td>
        <td>Amount</td>
    </tr>
    @foreach ($claims as $idx => $claim)
    <tr>
        <td>{{ $idx }}</td>
        <td>{{ optional($claim)->FullName ?? 'No FullName' }}</td>
        <td>{{ optional($claim)->BankCode ?? 'No BankCode' }}</td>
        <td>{{ number_format(optional($claim)->amount ?? 0, 2, '.', ',') }}</td>
    </tr>
    @endforeach
@endforeach
Brian Lee
  • 17,904
  • 3
  • 41
  • 52
  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName – shhrzl Jan 03 '19 at 05:22
  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete. – Brian Lee Jan 03 '19 at 05:34
  • `data_get` and `data_set` are helper methods in Laravel for getting and setting data in arrays / objects. `data_get` will return null if the provided index is not set / null. – Brian Lee Jan 03 '19 at 05:37
  • is it correct syntax `data_set($groups, $item->EntityName, [ ...$group, $item ]);`? it has `...$group` – shhrzl Jan 03 '19 at 05:42
  • `...` is the [spread operator](https://stackoverflow.com/questions/45419150/php-spread-operator-in-array-declaration), which destructures the array. – Brian Lee Jan 03 '19 at 05:43
  • `syntax error, unexpected '...' (T_ELLIPSIS), expecting ']'` i'm using php 5.6 – shhrzl Jan 03 '19 at 05:47
  • You can use`array_merge($group, [$item]);` instead. – Brian Lee Jan 03 '19 at 05:49