dimanche 21 août 2016

How to add specific numbers within an array in php

$a = array(2, 6, 24, 16, 7, 10);

I know how to add all the numbers using array_sum() but if I want to add only the numbers between 2 and 16 how can do that?

This is one of the solution that that I've come up with:

    $a = array(1, 3, 4, 6, 7);
    $r = array_slice($a, 0, -2);
    print_r (array_sum($r));

Just want to know if there is any other way to get the result.

Aucun commentaire:

Enregistrer un commentaire