3

I have tried using this example here Return index of highest value in an array

But that does not go into a multi dimensional Array

I have tried ARRAY_COLUMN(), array_values(), array_shift, Max and Min but they are not getting the info i need

I want to be able to loop thru and get to:

[pricing]

then Check pricing to see which is the highest and lowest in the nested array

so like below which total was the Highest and lowest trades on these dates

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

obviously [2017-09-23] is a simple one, i just dint want to add much more code for ppl helping to go thru.

The Array i create looks like this:

 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
            )

    )

in the example above i would like it to Create a new array with only the following

date -> last timestamp -> Highest pricing -> Lowest lowest -> Total of all Totals -> first Timestamp

EDIT: the last and first Timestamp are basically the first and last index so in this case:

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

or Index [0] and index [3]

Dynamite Media
  • 159
  • 2
  • 10

1 Answers1

3

The array from your question:

$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',
    ),
  ),
);

To get the values maybe you can use array_map and with this the function array_column is the key of all, try with this:

$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 is an array with the filter values that you need:

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

Get the pricing of the [first_timestamp] and [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);

I added an array to both timestamps with the value of these and the pricing.

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
                )

        )

)
kip
  • 1,120
  • 7
  • 11