www.webdeveloper.com
Results 1 to 10 of 10

Thread: Sum of arrays?

  1. #1
    Join Date
    Jun 2009
    Posts
    34

    Sum of arrays?

    Hi,

    Googling and lots of trying, didnt really get me anywhere to what i would like, hopefully someone can help point me in the right direction.

    For testing purposes i have the following array:

    PHP Code:
    $test = array(array(a,1,2,3,4),array(b,1,2,3,4),array(b,1,2,3,4)); 
    I would like to create a function, that adds up the values (the numbers) of each array, if the first value already exists in the array (the letters), so that above posted array, would become:

    [PHP}
    $testAgain = array(array(a,1,2,3,4),array(b,2,4,6,8));
    [/PHP]

    I've tried to first get the letters in a new array, and make sure that's unique, and then go through $test again, to, if a letter matches, add the values up...but well, that doesnt really seem to work.

    Anyone got a hint for me?

    Thanks!


    peace

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    array_sum() does that as for your array structure, wouldn't
    PHP Code:
    $test = array( "a"=>array(1,2,3,4),"b"=>array(1,2,3,4),"c"=>array(1,2,3,4) );
    $a array_sum$test["a"] );
    echo 
    "Sum of values in array 'a' = ".$a
    work for you?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    This might be a case of needing to understand the context, and perhaps finding a very different solution. For instance, if this array you're dealing with is the result of a database query, my suspicion is that it could be handled more easily and efficiently at the database level, such as with a GROUP BY query with SUM() functions for each of those numeric fields.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jun 2009
    Posts
    34
    Hi,

    Thanks for your quick reply.

    You solution wouldnt work for me though, it gets the sum of the array, where i would like to add up values. The array (key?) holds an article number, the numbers are quantities of variations of article; if an article number is being inserted in the array twice, i would like to add up the quantities, perhaps something looking like:

    PHP Code:
    $test = array ("cube"=>array("red"=>1,"blue"=>1), "ball"=>array("red"=>1,"blue"=>1), "cube"=>array("red"=>1,"blue"=>2) ); 
    As it is now, the php just echoes what it sees, so it would list "cube" twice, with its quantities, where i would like to have it listed just once, with the quantities added up, so that the array would look like:

    PHP Code:
    $test = array ("cube"=>array("red"=>2,"blue"=>3), ("ball"=>array("red"=>1,"blue"=>1) ); 
    Is this possible or is there another way of achieving this? Perhaps already "sort" the array when inserting the new values?

    peace

  5. #5
    Join Date
    Jun 2009
    Posts
    34
    The array is not a result of a database query. I get the data from a form, which saves the data in a (temp) array, which gets pushed into the array i want to sort ($test in this case)



    peace

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    Something like this, perhaps?
    PHP Code:
    <?php
    $test 
    = array(
       array(
    'a'123),
       array(
    'b'246),
       array(
    'b'345),
       array(
    'a'101)
    );
    $sorted = array();
    foreach(
    $test as $entry)
    {
       
    $key array_shift($entry);
       if(isset(
    $sorted[$key]))
       {
          foreach(
    $entry as $ix => $value)
          {
             
    $sorted[$key][$ix] += $value;
          }
       }
       else
       {
          
    $sorted[$key] = $entry;
       }
    }
    echo 
    "<pre>".print_r($sorted1)."</pre>";
    Output:
    Code:
    Array
    (
        [a] => Array
            (
                [0] => 2
                [1] => 2
                [2] => 4
            )
    
        [b] => Array
            (
                [0] => 5
                [1] => 8
                [2] => 11
            )
    
    )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jun 2009
    Posts
    34
    Hi,

    That is exactly what im looking for!

    Unfortunately i have to go to work in a minute, so i don't have time to try it out at the moment, but i will when i get back, and will let you know!

    Thanks ever so much!


    peace

  8. #8
    Join Date
    Jun 2009
    Posts
    34
    The script NogDog posted works well for me, however i run into the problem of how to echo the key and the values.

    I would like to echo out the key (the product id) and the values (quantities of different variations of that product), something like:

    Product: a, Red: 2, Blue, 3, Yellow, 4

    I've been trying with key(), but that only returns 0 ?? I've been trying quite a few things, obviously also googled it and searched on the forums here, but i cant seem to find the answer i'm looking for. I either get no result, 0 or 'Array' when i echo.


    Would appreciate it a lot if anyone could help me with some directions!



    Thanks a lot!


    peace

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    In my previous example, you could do:
    PHP Code:
    foreach($sorted as $id => $data)
    {
       echo 
    "<h4>Product #$id</h4>\n<ul>\n";
       foreach(
    $data as $key => $total)
       {
          echo 
    "<li>$key$total</li>\n";
       }
       echo 
    "</ul>\n";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Jun 2009
    Posts
    34
    Thanks a lot NogDog, that works perfectly for me!

    Fortunately, im almost done with the page im working on. A very heavy delivery as i'm pretty new to php and i'm trying to learn as i go (i have a different scripting background, but eager to learn PHP as well)

    Now all i need to do is sort out this session issue, and i think im good to go.

    Thanks again!


    peace

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles