Provides additional array functionality, augmenting the built in array_*
functions for use in common descriptive statistic calculations.
This library requires PHP 7.1 or newer, including 8.0, 8.1, and 8.2.
composer require giberti/array-functions
Creates a fingerprint for the array, useful for caching values.
$values = [1,2,2,3];
$fingerprint = array_fingerprint($values);
echo $fingerprint; // f591c5a8a39f752a2040e2364e775aec
Groups array values into buckets suitable for source data for a histogram. Takes an optional parameter to force the number of buckets the content should be distributed into.
$values = [1,2,2,3,3,3];
$buckets = array_bucket($values);
print_r($buckets);
// Array (
// [[0.5,1.5)] => 1
// [[1.5,2.5)] => 2
// [[2.5,3.5]] => 3
// )
Finds the mean (average) value of the elements in an array of numeric values.
$values = [1,2,2,3];
$mean = array_mean($values);
echo $mean; // 2
Finds the difference between the minimum value and the maximum value in the array.
$values = [1,2,3];
$difference = array_range($values);
echo $difference; // 2
Finds the variance for a given array. Works with populations as well as samples.
$values = [1,2,2,3];
$variance = array_variance($values);
echo $variance; // 0.66666666666667
$standardDeviation = sqrt($variance);
echo $standardDeviation; // 0.81649658092773