0

I'm working on SMA(simple moving average) function for 5 second ( find the best average of every 5 values for one by one from an array).
I have over 1000 arrays as a result but here we only have 2 for instance.

 0 => array:5 [▼
        1608616431 => 54
        1608616432 => 71
        1608616433 => 79
        1608616434 => 75
        1608616435 => 100
      ]
      1 => array:5 [▼
        1608616432 => 71
        1608616433 => 79
        1608616434 => 75
        1608616435 => 100
        1608616436 => 99
      ]

I'd like to find the maximum of the averages from these arrays and I'm using array_sum then apply max function over them.

for the above arrays we will have avg as follows :

Avgs=[78.8,84.8]

so the maximum would be : 84.8

I need the first key of the array which the maximum comes from it, for this example would be 1608616432

Ehsan Pro
  • 15
  • 3
  • Not clear what your case is, how this calculation is done or what the resulting array is. Please share your code, explain what you expect the code to return and where and how what you're getting is different from what you need. – El_Vanja Mar 01 '21 at 14:54
  • @El_Vanja it's updated. – Ehsan Pro Mar 01 '21 at 15:05
  • Still doesn't show how you calculate the averages. But it seems it would boil down to simply manually defining the keys in the resulting array. – El_Vanja Mar 01 '21 at 15:06
  • Besides that, what do you want as the result when multiple averages have the same max value? Do you want all of them? Earliest? Latest? – El_Vanja Mar 01 '21 at 15:08
  • 1
    Why do you have overlapping arrays here to begin with, why is this not _one_ array that contains all the entries from 1608616431 up to 1608616436 directly? – CBroe Mar 01 '21 at 15:22
  • @cBroe As I noted, we have to apply sma function.it made it grouped by 5items then sum,then get the average. – Ehsan Pro Mar 01 '21 at 16:05
  • But it is not _necessary_ to hack this into individual arrays for that purpose. You can just loop over the full array, and calculate the average for five items in a row each time, starting with the current one. – CBroe Mar 02 '21 at 07:21

2 Answers2

0

When doing array_sum(), you could write the result in an indexed array, e.g. with array_key_first().

Then when getting max() you can use array_search() to find the (first) timestamp matching the maximum value.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • thank you,i just updated the question ,it gives you better understanding, we have to find key from prevoius array which its avg is maximum among another arrays. – Ehsan Pro Mar 01 '21 at 15:07
0

This script will do exactly what you want

<?php

$array = [
    [
        '1608616431' => 54,
        '1608616432' => 71,
        '1608616433' => 79,
        '1608616434' => 75,
        '1608616435' => 100
    ],
    [
        '1608616432' => 71,
        '1608616433' => 79,
        '1608616434' => 75,
        '1608616435' => 100,
        '1608616436' => 99,
    ],
];

$elementAverage = [];
foreach ($array as $index => $element) {
    $average                                  = array_sum($element)/count($element);
    $elementArrayKeys                         = array_keys($element);
    $elementAverage[reset($elementArrayKeys)] = $average;
}

echo array_search(max($elementAverage), $elementAverage);

output

1608616432

First we make $elementAverage and then get the max average
Then just use the index and get what I want

azibom
  • 1,769
  • 1
  • 7
  • 21
  • 1
    Or if you used the first key of `$element` instead of `$index`, you would have the timestamp in `$elementAverage` and `array_search(max($elementAverage), $elementAverage)` would then directly return it. – El_Vanja Mar 01 '21 at 15:30
  • Nice, you are completely right, I update my answer :) – azibom Mar 01 '21 at 15:35