0

NOTE: I am sorry if this question has been asked. but the ones that have been asked and answered do not directly address the condition in my question. that is why i have asked it.

I have this code that sorts my list from lowest value to highest value and then it sums the first three But the variables keep changing

<?php $zero = 0; $one = 1;  $two = 2; $three = 3; $four = 4;   ?>
<?php 
    $arr = array($zero,$one,$two,$three,$four);
    asort($arr);                    // sort ascending, lowest first
    $tre = array_slice($arr, 0, 3);  // first three
    $sum = array_sum($tre);          // summed up
?>

<div id="result"><?php echo $sum; ?></div>

What this code does, is that it sums up the first three numbers in ascending order including the zero i.e.

0+1+2 = 3

But that is not what i want, I would want it to sum up the first three numbers in ascending order but to ignore the zeros i.e. in my case

1+2+3 = 6

in addition: if a code can eliminate zero and also an empty value, it could be much better but not necessary

Any help on how i could go about this?

Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
  • 2
    You can use `array filter` as this clever [gentleman](http://stackoverflow.com/a/2287415/5473628) pointed out. – peaceman Mar 11 '16 at 11:22
  • @peaceman but it does not directly answer my question. I had looked at it earlier but i am sorry i could not understand it. that is why i had to ask the question. – Omari Victor Omosa Mar 11 '16 at 11:33
  • use it like this $arr = array_filter($arr) if you just supply it with an array it will filter out the values equal to false. 0 is equal to false so zeros will be filtered out. – peaceman Mar 11 '16 at 11:40
  • @Anant i have. You have helped me a lot – Omari Victor Omosa Mar 11 '16 at 11:48

6 Answers6

2

First why to create so much variables,just create an array with values directly (i showed in my example)

You can remove your 0 value from array using array_filter():-

  <?php 
    $arr = array(0,1,2,3,4);

    $arr = array_filter($arr);      // remove 0 values
    asort($arr);                    // sort ascending, lowest first
    $tre = array_slice($arr, 0, 3);  // first three
    $sum = array_sum($tre);          // summed up
?>

<div id="result"><?php echo $sum; ?></div>

Output:- https://eval.in/534727

Reference:- http://php.net/array_filter

Or Just change in your codearray_slice($arr,1,3) (BUT IT WILL WORK ONLY WHAT THE CODE YOU SHOWN TO US NOT IN OTHER CASES):-

<?php 
    $arr = array(0,1,2,3,4);
    asort($arr);                    // sort ascending, lowest first
    $tre = array_slice($arr, 1, 3);  // first three
    $sum = array_sum($tre);          // summed up
?>

<div id="result"><?php echo $sum; ?></div>

Output:-https://eval.in/534704

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2

Try this:-

<?php
$arr = array(1,3,45,2,7,0,1,0);
echo removeZeroAndSumFirstThree();

function removeZeroAndSumFirstThree($arr) {
    asort($arr);
    //Remove 0 from array
    $a = array_diff($arr, array(0));
    $output = array_slice($a, 0, 3);
    return array_sum($output);
}
Ravindra Bhalothia
  • 1,720
  • 2
  • 13
  • 16
1

Personally I would write a helper function that iterates through the array that is re usable:

<?php

public function sumFirstNonZeros($array){
    $countAdded = 0;
    $sum = 0;
    foreach($array as $number){
        if($countAdded<3){
            if($number > 0){
                $sum = $sum + $number;
                $countAdded = $countAdded + 1;
            }
        } else {
            break;
        }
    }

    return $sum;
}

?>
Joe Hart
  • 78
  • 2
  • 7
1
$arr = array($zero,$one,$two,$three,$four);
asort($arr);                    // sort ascending, lowest first
$sum = $i = 0;
foreach ($arr as $num) {

    if($num != 0) {
        $sum == $sum + $num;
        $i++;
    }
    if ($i == 2) {
        break;
    }
}
echo $sum;

or you can try this

$arr = array($zero, $one, $two, $three, $four);
asort($arr);                    // sort ascending, lowest first

$sumArr = array();
foreach ($arr as $num) {
    if ($num != 0) {
        $sumArr[] = $num;
        if (count($sumArr) == 3) {
            break;
        }
    }
}
echo array_sum($sumArr);
Govind
  • 314
  • 1
  • 11
0

More universal solution:

EDIT:

Correct one:

$zero = 0; $one = 1;  $two = 2; $three = 3; $four = 4;  

$arr = array($zero,$one,$two,$three,$four);
asort($arr);                    // sort ascending, lowest first
$total = 0;
$i = 0;

foreach ($arr as $v)
{
    if ($i < 3)
    {
  if ($v != 0)
  {
    var_dump($v);
    $total += $v;
    $i++;
  }
    }
}

echo $total;
Qrzysio
  • 1,147
  • 3
  • 12
  • 25
0

you can also use array_filter() function to remove zero values from array before the sum.

http://php.net/manual/en/function.array-filter.php