PHP code example of square1 / laravel-collection-rolling-average
1. Go to this page and download the library: Download square1/laravel-collection-rolling-average library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
square1 / laravel-collection-rolling-average example snippets
$data = new Collection([1, 2, 3, 4, 5]);
$averages = $data->rollingAverage();
/**
* 1, // 1
* 1.5, // 1+2 / 2 (only 2 entries seen so far)
* 2, // 1+2+3 / 3
* 2.5, // 1+2+3+4 / 4
* 3 // 1+2+3+4+5 / 5
*/
$data = new Collection([1, 2, 3, 4, 5, 6]);
// How many entries should we consider in our average?
$lookback = 2;
$averages = $data->rollingAverage($lookback);
/**
* Looking back over last 2 entries, averages should be:
* 1, // 1
* 1.5, // 1+2 / 2
* 2.5, // 2+3 / 2
* 3.5, // 3+4 / 2
* 4.5, // 4+5 / 2
* 5.5 // 5+6 / 2
*/
php
$data = new Collection([1, 2, 3, 4, 5, 6]);
$averages = $data->rollingAverage(4, $enforceLimitedLookback = true);
/**
* 2.5,
* 3.5,
* 4.5
*/