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?

4
  • 2
    You can use array filter as this clever gentleman pointed out. Commented Mar 11, 2016 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. Commented Mar 11, 2016 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. Commented Mar 11, 2016 at 11:40
  • @Anant i have. You have helped me a lot Commented Mar 11, 2016 at 11:48

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Does array_filter($arr); automaticaly eliminate the zeros
What if you've more 0 in array.. You're first example fails in that case. array_filter works fine in that case.
<?php $arr = array(0,1,2,0,0,3,4); asort($arr); // sort ascending, lowest first $tre = array_slice($arr, 1, 3); // first three $sum = array_sum($tre); echo $sum; Try array_slice code using $arr = array(0,1,0,0,2,1,4); and let me know the answer please
I was talking about another one dear in which you said you can do array_slice($arr, 1,3); i already said that your array_filter works fine in that case.
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);
}

3 Comments

why custom code if already an inbuilt function can do that array_filter();
I think you missed argument of function. Giving error while calling function 3v4l.org/AIU2F
For php < 5.4 define array in traditional manner i.e. $var = array(..) to avoide syntax error
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;
}

?>

Comments

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);

Comments

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

Comments

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;

1 Comment

@Qrzysio thanks so much, that could be of some help to me at some scenario

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.