#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    How to array_sum all arrays from DB


    [PHPNET]<?php
    $i = 1;
    foreach ($cjitems as $itemId => $item)
    {

    $unitDiscount = round($item->getDiscountAmount(), 2);


    $itemDct .= $unitDiscount . ',' ;
    $b = array($itemDct);
    $a = array(2, 4, 6, 8,);

    $i++;
    }
    ?>

    <?php echo array_sum($b); ?>
    [/PHPNET]


    Hi!

    Please help, If I echo $itemDct it will display for example 3,0,7,0,
    So I want those numbers to be added together.
    Now echo array_sum($b); will only show the first digit number 3 and not a sum of all 4 values 10.

    Please help!!

    Many thanks in advance.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    PHP Code:
    <?php echo array_sum(explode(','$b[0]));?>
    Or
    PHP Code:
    <?php
    $itemDct 
    .= $unitDiscount ',' ;
    $b[] = $unitDiscount;

    //...

    echo array_sum($b);
    Ooh, they have the Internet on computers now!
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Hi,

    the whole code makes no sense, because $a and $b are overwritten in each iteration. What's the point of $a, anyway? The comma separated list also looks rather dubious.

    You need to get clear about what you want to do and then write your code accordingly. If you just want to sum the elements of $cjitems, make a loop and add each element to a variable:
    PHP Code:
    $sum 0;
    foreach (
    $cjitems as $item
        
    $sum += round($item->getDiscountAmount(), 2); 
    Keep in mind that programming languages are stupid and only do what you tell them. You cannot pass a string "a,b,c,..." to array_sum() and then expect it to somehow guess what you mean.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Hi there!

    I just added $a to see if that works and it did work.

    Now I want $b to be all added and make a sum of 4 digit numbers.

    Thank you guys for codes I am trying right now and will post the result!!!!

    Originally Posted by Jacques1
    Hi,

    the whole code makes no sense, because $a and $b are overwritten in each iteration. What's the point of $a, anyway? The comma separated list also looks rather dubious.

    You need to get clear about what you want to do and then write your code accordingly. If you just want to sum the elements of $cjitems, make a loop and add each element to a variable:
    PHP Code:
    $sum 0;
    foreach (
    $cjitems as $item
        
    $sum += round($item->getDiscountAmount(), 2); 
    Keep in mind that programming languages are stupid and only do what you tell them. You cannot pass a string "a,b,c,..." to array_sum() and then expect it to somehow guess what you mean.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Omg. [PHPNET=" <?php echo array_sum(explode(',', $b[0]));?> "] <?php echo array_sum(explode(',', $b[0]));?> [/PHPNET] worked like a charm. Thank you so much
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Originally Posted by Seongtaek
    Omg. [PHPNET=" <?php echo array_sum(explode(',', $b[0]));?> "] <?php echo array_sum(explode(',', $b[0]));?> [/PHPNET] worked like a charm. Thank you so much
    There are a lot of things that "work". The problem is, it makes no sense to first cram all values into a string and then extract them again to get the sum. This is extremely unclean and inefficient.

    If you actually need this strange comma separated list, then don't use it to calculate the sum. Do the adding in the loop:
    PHP Code:
    <?php

    $i 
    1;                                                            // no idea what's that for
    $sum 0;
    foreach (
    $cjitems as $item
    {
        
    $unitDiscount round($item->getDiscountAmount(), 2);
        
    $itemDct .= $unitDiscount ',';                            // no idea what's that for
        
    $sum += $unitDiscount;
        
    $i++;
    }

IMN logo majestic logo threadwatch logo seochat tools logo