find the standard deviation of all keys in a map in php

One way to find the standard deviation of all keys in a PHP map (associative array) is by using the array_keys() function to extract all the keys into a new array, and then using the stats_standard_deviation() function from the PHP math extension (which must be installed and enabled on your server) to calculate the standard deviation:

main.php
<?php
// define a sample associative array
$data = array(
    'apple' => 2,
    'banana' => 3,
    'orange' => 4,
    'pear' => 5,
    'grape' => 6,
);

// extract the keys into a new array
$keys = array_keys($data);

// calculate the standard deviation of the keys
$stdev = stats_standard_deviation($keys);

echo $stdev; // outputs: 1.5811388300842
?>
353 chars
19 lines

In this example, the output is 1.5811388300842, which is the standard deviation of the keys in the $data array.

gistlibby LogSnag