1

I am a rookie beginner with PHP, i was wondering how i could add up the total number from 1 array + the total number of another array together. I managed to make this code with help from stackoverflow answers on google. I don't know why but it's no where explained or i am looking over it. Been looking for almost an hour to make this work. Here is the code:

<?php 

$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);

$odds = array();
$even = array();

foreach($array as $val) {
    if($val % 2 == 0) {
        $even[] = $val;
    } else {
        $odds[] = $val;
    }
}



$array = array();
foreach($even as $key => $val) {
    $array[] = $val;
    if(isset($odds[$key])) {
        $array[] = $odds[$key];
    }
}

echo '<b>Oneven</b> ';
print_r($odds);

echo '<br><br><br>';

echo "Bovenstaande <b>oneven</b> getallen bijelkaar opgeteld = " . array_sum($odds) . "\n";
echo '<br><br><br><hr style="margin-top:2%;margin-bottom:4%;">';

/* Array nummer 2 */


$array = array(20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40);

$odds = array();
$even = array();

foreach($array as $val) {
    if($val % 2 == 0) {
        $even[] = $val;
    } else {
        $odds[] = $val;
    }
}

$array = array();

foreach($even as $key => $val) {
    $array[] = $val;
    if(isset($odds[$key])) {
        $array[] = $odds[$key];
    }
}
echo '<b>Even</b> ';
print_r($even);
echo '<br><br><br>';

echo "Bovenstaande <b>even</b> getallen bijelkaar opgeteld = " . array_sum($even) . "\n";



?>

So i don't know how to do it in another way but i have array 1 code at first and then code 2 begins with another array.

The thing is that i want to make a program that includes the odd numbers from 1 to 19 and the even numbers from 20 to 40 and then count the total of those 2 array's. Is there a way to do this in 1 code and count up the total of those 2 array's together. I already have that part of code that it counts the array, in code 1 that is 100 and in code 2 it is 330.

330+100=430 that's the output that i want. Why is that so hard? haha...

I appreciate the help and time effort.

2
  • 1
    How about array_merge()ing , then array_sum()ing? Something like that? Commented Apr 10, 2018 at 18:28
  • Just add another condition where you check if the number is even or odd to also check if the number is < or >= 20 Commented Apr 10, 2018 at 18:29

1 Answer 1

1

First off, there's a lot of complexity involved in creating the initial array and then extracting only the odd numbers. This complexity can be eliminating by using the range and array_filter functions like so:

$odds = array_filter(range(1, 19), function($elem) {
    return $elem & 1;
});

$even = array_filter(range(20, 40), function($elem) {
    return $elem % 2 == 0;
});

to calculate sum of the sum of odds plus the sum of even, you can simply merge them together and use array_sum in the same you are doing for the individual arrays

$totalSum = array_sum(array_merge($odds, $even))

As @Darragh pointed out in the comments, you can simplify the array creation by specifying a step parameter for the range function.

$odds = range(1, 19, 2) // start at 1, go up to 19, by increments of 2
Sign up to request clarification or add additional context in comments.

3 Comments

range() accepts a third "step" argument, so you can alternatively create a sequence of odd numbers with $odds = range(1, 19, 2); and sequence of even numbers with $evens = range(20, 40, 2);
@DarraghEnright Did not know that, I'll include it in the answer
Thanks for the explanation guy's it is so fun to start this adventure on PHP. Thank's to the explanation from you guy's! In dutch we say 'TOP' that means 'perfect/great' haha

Your Answer

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