I can’t seem to get array_sum to work right, maybe somebody can help me.
foreach ($this->vacation as $v) {
$this->all = array();
$this->all[] = ((strtotime("$this->end 23:59:59") - strtotime("$this->beginning")) / 86400) - $this->sum_of_days;
}
$this->all = is_array($this->all) ? array_sum($this->all) : 0;
After the foreach loop I would like to sum all the values in the array, but it does not sum, it just gives me the last value of the array. I am trying to write my first PHP class and I am just getting frustrated with this array.
You are initializing the array inside the
foreachloop, which means that you are creating an empty arrray in the first loop and then adding an element. In the second loop you are creating the array again (thus eliminating the first element) and adding the an element. So at the end of each loop your array just contains one element. Move the initialization outside the loop: