-1

I have an array that is the one below.

I need to be able to display it like a table with first columns on top being Agent Names. The columns on the left displaying dates, and sums in the records where they intersect. I understand how to step through and add HTML in a For Each.. However I am having trouble with the sorting to have it appear correct.. Something like the table below:

--------------1-23-2018---------------1-24-2018---------------------

Daniel ---$1,995.00 ----------------$1,459.00

Gail ------$1,695.00 ----------------$3,845.00


I understand how to step through

Please Help. I am doing all this in PHP.

Array (
[2018-01-23] => Array
    (
        [0] => Array
            (
                [0] => Daniel
                [Agent] => Daniel
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 1995.00
                [Total] => 1995.00
            )

        [1] => Array
            (
                [0] => Gail
                [Agent] => Gail
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 1695.00
                [Total] => 1695.00
            )

        [2] => Array
            (
                [0] => Joe
                [Agent] => Joe
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 395.00
                [Total] => 395.00
            )

        [3] => Array
            (
                [0] => Judy
                [Agent] => Judy
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 1795.00
                [Total] => 1795.00
            )

        [4] => Array
            (
                [0] => Justin
                [Agent] => Justin
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 2390.00
                [Total] => 2390.00
            )

        [5] => Array
            (
                [0] => Kevin
                [Agent] => Kevin
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 1800.00
                [Total] => 1800.00
            )

        [6] => Array
            (
                [0] => Sky
                [Agent] => Sky
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 795.00
                [Total] => 795.00
            )

        [7] => Array
            (
                [0] => Tony
                [Agent] => Tony
                [1] => 2018-01-23
                [Deal_Date] => 2018-01-23
                [2] => 1695.00
                [Total] => 1695.00
            )

    )

[2018-01-25] => Array
    (
        [0] => Array
            (
                [0] => Daniel
                [Agent] => Daniel
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 4590.00
                [Total] => 4590.00
            )

        [1] => Array
            (
                [0] => Gail
                [Agent] => Gail
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 3845.00
                [Total] => 3845.00
            )

        [2] => Array
            (
                [0] => Joe
                [Agent] => Joe
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 495.00
                [Total] => 495.00
            )

        [3] => Array
            (
                [0] => Justin
                [Agent] => Justin
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 945.00
                [Total] => 945.00
            )

        [4] => Array
            (
                [0] => Kevin
                [Agent] => Kevin
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 1995.00
                [Total] => 1995.00
            )

        [5] => Array
            (
                [0] => Kyle
                [Agent] => Kyle
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 1790.00
                [Total] => 1790.00
            )

        [6] => Array
            (
                [0] => Lisa
                [Agent] => Lisa
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 1995.00
                [Total] => 1995.00
            )

        [7] => Array
            (
                [0] => Sam
                [Agent] => Sam
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 2990.00
                [Total] => 2990.00
            )

        [8] => Array
            (
                [0] => Sky
                [Agent] => Sky
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 995.00
                [Total] => 995.00
            )

        [9] => Array
            (
                [0] => Tony
                [Agent] => Tony
                [1] => 2018-01-25
                [Deal_Date] => 2018-01-25
                [2] => 995.00
                [Total] => 995.00
            )
3
  • 1
    Possible duplicate of PHP multidimensional array to html table Commented Feb 17, 2018 at 2:32
  • Try something like this: stackoverflow.com/questions/13767211/… & Moving forward please provide a minimal reproducible example of what you've tried on your own to solve this problem. Commented Feb 17, 2018 at 2:33
  • 2
    We could probably do a great job of helping you, starting from the query, if you provide an sqlfiddle link. It looks like your results set is _array but only needs to be _assoc. Commented Feb 17, 2018 at 4:18

1 Answer 1

2

What you are trying to create is a Pivot table. In general, it is not a simple task to do (that is one of the killer features of spreadsheets (i.e. Excel)), but in your particular case you can do the following:

// Flatten array, maybe redundant if you get data in appropriate format.
$flatten = array_reduce($array, 'array_merge', []);

// Store all unique dates in the index lookup table (j).
$dates = array_flip(array_values(array_unique(array_column($flatten, 'Deal_Date'))));
// Store all unique names in the index lookup table (i).
$names = array_flip(array_values(array_unique(array_column($flatten, 'Agent'))));

$totals = array_reduce(
    $flatten,
    function ($totals, $item) use ($names, $dates) {
        $i = $names[$item['Agent']];
        $j = $dates[$item['Deal_Date']];
        $totals[$i][$j] += $item['Total'];

        return $totals;
    },
    // Prefil table with zeros.
    array_fill(0, count($names), array_fill(0, count($dates), 0))
);

// Displaying part:
// Merge all the raws and cols together (add headers).
$table = $totals;
array_unshift($table, array_flip($dates));
$names = array_flip($names);
array_unshift($names, null);
$table = array_map(function ($name, $row) {
    array_unshift($row, $name);

    return $row;
}, $names, $table);

// Calculate max widthes for better displaying:
$width = array_map(function ($column) {
    return max(array_map('strlen', $column));
}, array_map(null, ...$table));

foreach ($table as $row) {
    echo implode(' | ', array_map(function ($cell, $width) {
        return str_pad($cell, $width);
    }, $row, $width)), PHP_EOL;
}

This will give you a table like the following:

       | 2018-01-23 | 2018-01-25
Daniel | 1995       | 4590      
Gail   | 1695       | 3845      
Joe    | 395        | 495       
Judy   | 1795       | 0         
Justin | 2390       | 945       
Kevin  | 1800       | 1995      
Sky    | 795        | 995       
Tony   | 1695       | 995       
Kyle   | 0          | 1790      
Lisa   | 0          | 1995      
Sam    | 0          | 2990 

Here is the demo.

For a more general solution, please, consider using some dedicated library, for example, this one.

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.