Home > database >  how to show 3 Array in one datatable
how to show 3 Array in one datatable

Time:03-18

I am trying to show 3 Array in one datatable.

// Combine Month Name and Data Array
$incomedataforyear  = array();
$test1           = array_combine($getmonths, $incomedata);
$test2           = array_combine($getmonths, $animalselldata);
$test3           = array_combine($getmonths, $otherselldata); 

$collection = collect([$test1, $test2, $test3]);
// End of Combine Month Name and Data Array

I also tried to use Collection but dont have any knowledge how to use this.

datatable code

<table id="incometable"  data-order='[[ 0, "desc" ]]'>
    <thead>
        <tr>
            <th>Month</th>
            <th>Milk Sale Amount (Rs.)</th>
            <th>Animal Sale Amount (Rs.)</th>
            <th>Other Sale Amount (Rs.)</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($expdataresults as $item )
            <tr> 
                <td>00</td>
                <td>00</td>
            </tr>
        @endforeach
    </tbody>
</table>

Thanks in Advance

enter image description here enter image description here

CodePudding user response:

Formatting a bit different, you could use array_merge_recursive

$test1 = array_combine($getmonths, array_map(fn($i) => ['incomedata' => $i], $incomedata));
$test2 = array_combine($getmonths, array_map(fn($i) => ['animalselldata' => $i], $animalselldata);
$test3 = array_combine($getmonths, array_map(fn($i) => ['otherselldata' => $i], $otherselldata); 

$collection = collect(array_merge_recursive($test1, $test2, $test3));
@foreach ($collection as $key => $value)
    <tr>
        <td>{{ $key }}</td>
        <td>{{ $value['incomedata'] }}</td>
        <td>{{ $value['animalselldata'] }}</td>
        <td>{{ $value['otherselldata'] }}</td>
    </tr>
@endforeach
  • Related