giberti/array-functions

Helpers for calculating descriptive statistics on arrays.

v0.2.0 2018-11-03 23:14 UTC

This package is auto-updated.

Last update: 2024-04-21 14:47:02 UTC


README

Provides additional array functionality, augmenting the built in array_* functions for use in common descriptive statistic calculations.

Build and Test

Installing

This library requires PHP 7.1 or newer, including 8.0, 8.1, and 8.2.

composer require giberti/array-functions

Usage

string array_fingerprint($array)

Creates a fingerprint for the array, useful for caching values.

$values = [1,2,2,3];
$fingerprint = array_fingerprint($values);
echo $fingerprint; // f591c5a8a39f752a2040e2364e775aec

float[] array_bucket($array, [$buckets = null])

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

float array_mean($array)

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

float array_range($array)

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

float array_variance($array [, $sample = true])

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