Created
July 17, 2019 23:54
-
-
Save parzibyte/d6a985de4cb3c05d8da524368decbad9 to your computer and use it in GitHub Desktop.
Promedio de arreglo de PHP con array_reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://parzibyte.me/blog | |
$numeros = array(12, 34, 554, 123, 15); | |
$valorInicial = 0; // Valor inicial de array_reduce | |
$suma = array_reduce($numeros, function ($acarreo, $numero) { | |
return $acarreo + $numero; | |
}, $valorInicial); | |
// Obtener longitud | |
$cantidadDeElementos = count($numeros); | |
// Dividir, y listo | |
$promedio = $suma / $cantidadDeElementos; | |
echo "Promedio: $promedio"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment