如何从PHP中的多维数组中获取最高和最低值和总和

时间:2017-09-24 01:34:34

标签: php arrays multidimensional-array

我在此尝试使用此示例Return index of highest value in an array

但这不会进入多维数组

我尝试了ARRAY_COLUMN(),array_values(),array_shift,Max和Min,但他们没有得到我需要的信息

我希望能够循环播放并进入:

[定价]

然后检查定价以查看嵌套数组中哪个是最高和最低

如下所示,总数是这些日期的最高和最低交易

[2017-09-22] 
[2017-09-23] 

显然 [2017-09-23] 是一个简单的,我只是想为ppl添加更多代码,帮助我们通过。

我创建的数组如下所示:

 Array
   (
   [2017-09-23] => Array
    (
        [0] => Array
            (
                [timestamp] => 1506169387000
                [pricing] => 9.5470
                [qty] => 25
                [total] => 238.675
                [date] => 2017-09-23
            )

    )

[2017-09-22] => Array
    (
        [0] => Array
            (
                [timestamp] => 1506093083000
                [pricing] => 9.6300
                [qty] => 25
                [total] => 240.75
                [date] => 2017-09-22
            )

        [1] => Array
            (
                [timestamp] => 1506077220000
                [pricing] => 8.7190
                [qty] => 13
                [total] => 113.347
                [date] => 2017-09-22
            )

        [2] => Array
            (
                [timestamp] => 1506077109000
                [pricing] => 8.6800
                [qty] => 83
                [total] => 720.44
                [date] => 2017-09-22
            )

        [3] => Array
            (
                [timestamp] => 1506065258000
                [pricing] => 8.7100
                [qty] => 25
                [total] => 217.75
                [date] => 2017-09-22
            )

    )

在上面的示例中,我希望创建一个只包含以下

的新数组
date -> last timestamp -> Highest pricing -> Lowest lowest -> Total of all Totals -> first Timestamp

编辑:最后一个和第一个时间戳基本上是第一个和最后一个索引,所以在这种情况下:

[timestamp] => 1506093083000   and    [timestamp] => 1506065258000

或索引[0]和索引[3]

1 个答案:

答案 0 :(得分:3)

您问题中的数组:

keyDown

要获取值,您可以使用$array = array ( '2017-09-23' => array ( 0 => array ( 'timestamp' => '1506169387000', 'pricing' => '9.5470', 'qty' => '25', 'total' => '238.675', 'date' => '2017-09-23', ), ), '2017-09-22' => array ( 0 => array ( 'timestamp' => '1506093083000', 'pricing' => '9.6300', 'qty' => '25', 'total' => '240.75', 'date' => '2017-09-22', ), 1 => array ( 'timestamp' => '1506077220000', 'pricing' => '8.7190', 'qty' => '13', 'total' => '113.347', 'date' => '2017-09-22', ), 2 => array ( 'timestamp' => '1506077109000', 'pricing' => '8.6800', 'qty' => '83', 'total' => '720.44', 'date' => '2017-09-22', ), 3 => array ( 'timestamp' => '1506065258000', 'pricing' => '8.7100', 'qty' => '25', 'total' => '217.75', 'date' => '2017-09-22', ), ), ); 并使用此功能array_map是所有人的关键,请尝试使用此功能:

array_column

$values = array_map(function($dates) { $timestamps = array_column($dates, 'timestamp'); $pricings = array_column($dates, 'pricing'); return [ 'max_pricing' => max($pricings), 'lowest_pricing' => min($pricings), 'total_of_totals' => array_sum(array_column($dates, 'total')), 'first_timestamp' => reset($timestamps), 'last_timestamp' => end($timestamps), ]; }, $array); 是一个包含所需过滤器值的数组:

$values

修改

获取Array ( [2017-09-23] => Array ( [max_pricing] => 9.5470 [lowest_pricing] => 9.5470 [total_of_totals] => 238.675 [first_timestamp] => 1506169387000 [last_timestamp] => 1506169387000 ) [2017-09-22] => Array ( [max_pricing] => 9.6300 [lowest_pricing] => 8.6800 [total_of_totals] => 1292.287 [first_timestamp] => 1506093083000 [last_timestamp] => 1506065258000 ) ) [first_timestamp]

的定价
[last_timestamp]

我在两个时间戳中添加了一个数组,其中包含这些时间戳和价格。

$values = array_map(function($dates) {
    $timestamps = array_column($dates, 'timestamp');
    $pricings = array_column($dates, 'pricing');
    return [
        'max_pricing' => max($pricings),
        'lowest_pricing' => min($pricings),
        'total_of_totals' => array_sum(array_column($dates, 'total')),
        'first_timestamp' => [
            'value' => reset($timestamps),
            'pricing' => $pricings[each($timestamps)['key']]
        ],
        'last_timestamp' => [
            'value' => end($timestamps),
            'pricing' => $pricings[each($timestamps)['key']]
        ]
    ];
}, $array);