Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组以及如何按此数组顺序显示它_Php_Arrays - Fatal编程技术网

PHP数组以及如何按此数组顺序显示它

PHP数组以及如何按此数组顺序显示它,php,arrays,Php,Arrays,我有一个数组,就是下面的数组 我需要能够像一个表一样显示它,上面的第一列是代理名称。左边的列显示日期和它们相交的记录中的总和。我了解如何在For Each。。但是,我在排序时遇到了问题,无法使其看起来正确。。如下表所示: --------------2018年1月23日-----2018年1月24日--------------------- 丹尼尔-----1995.00美元-----1459.00美元 Gail-------1695.00美元--------3845.00美元 我知道如何通过

我有一个数组,就是下面的数组

我需要能够像一个表一样显示它,上面的第一列是代理名称。左边的列显示日期和它们相交的记录中的总和。我了解如何在For Each。。但是,我在排序时遇到了问题,无法使其看起来正确。。如下表所示:

--------------2018年1月23日-----2018年1月24日---------------------

丹尼尔-----1995.00美元-----1459.00美元

Gail-------1695.00美元--------3845.00美元


我知道如何通过

请帮忙。我正在用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
            )

您试图创建的是一个。一般来说,这不是一项简单的任务(这是电子表格(即Excel)的杀手级功能之一),但在您的特定情况下,您可以执行以下操作:

// 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;
}
       | 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 
这将为您提供如下表格:

// 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;
}
       | 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 
这是


对于一个更一般的解决方案,请考虑使用一些专用的库,例如,

可能的重复尝试这样的事情:向前迈进,请提供一个你自己尝试解决这个问题的方法。抱歉…我对这一切都不熟悉。我相信我已经完成了你的要求。没问题,干得好。欢迎加入团队。如果您提供SQLFIDLE链接,从查询开始,我们可能会为您提供很大的帮助。看起来您的结果集是
\u array
,但只需要是
\u assoc