2017-09-24 101 views
2

我已經在這裏使用這個例子Return index of highest value in an array如何從多維數組中的最高值和最低值和總和PHP

嘗試,但不進入一多維數組

我已經試過ARRAY_COLUMN() ,array_values(),array_shift,最大和最小,但他們沒有得到的信息,我需要

我希望能環通,並獲得:

[價格]

然後查看定價,看看哪些是最高和最低的嵌套數組中

所以像下面這總有人在上述日期

[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]

+0

最後的時間戳是按照數組值排序的最後一個值還是與其他時間戳比較的值? – kip

+0

我會編輯它! –

回答

3

從你的問題的陣列:

$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 
     ) 

) 

EDIT

獲取[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); 

定價我添加了一個陣列到兩個時間戳與這些值和定價。

Array 
(
    [2017-09-23] => Array 
     (
      [max_pricing] => 9.5470 
      [lowest_pricing] => 9.5470 
      [total_of_totals] => 238.675 
      [first_timestamp] => Array 
       (
        [value] => 1506169387000 
        [pricing] => 9.5470 
       ) 

      [last_timestamp] => Array 
       (
        [value] => 1506169387000 
        [pricing] => 9.5470 
       ) 

     ) 

    [2017-09-22] => Array 
     (
      [max_pricing] => 9.6300 
      [lowest_pricing] => 8.6800 
      [total_of_totals] => 1292.287 
      [first_timestamp] => Array 
       (
        [value] => 1506093083000 
        [pricing] => 9.6300 
       ) 

      [last_timestamp] => Array 
       (
        [value] => 1506065258000 
        [pricing] => 8.7100 
       ) 

     ) 

) 
+0

它沒有打印出來,我錯過了什麼? –

+0

使用'print_r($ values);' – kip

+0

這就是我做的...我明白了爲什麼,我沒有注意到你把它叫做$ array我是newArray,......在這個工作上花費大量時間!這工作很好,我認爲也會幫助別人 –

相關問題