Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 17, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/d6a985de4cb3c05d8da524368decbad9 to your computer and use it in GitHub Desktop.
Save parzibyte/d6a985de4cb3c05d8da524368decbad9 to your computer and use it in GitHub Desktop.
Promedio de arreglo de PHP con array_reduce
<?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