Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts

    how do i add values from one array to another.

    I will have two arrays like so...

    $tempCounters = array( 'userid' => '', array( 'itemOwnerCommentCount' => 0, 'notItemOwnerCommentCount' => 0 ) );

    and the main one being...

    $mainCounters = array( 'userid' => '', array( 'itemOwnerCommentCount' => 0, 'notItemOwnerCommentCount' => 0 ) );

    I am wanting to add the value in the temp array to the main array for each of the two parts of the array being the `itemOwnerCommentCount` and `notItemOwnerCommentCount`

    so if the temp has
    Code:
    userid				1	2	3
    itemOwnerCommentCount		2	1	0
    notItemOwnerCommentCount	4	1	2
    
    and the main array has
    userid				1	2	3
    itemOwnerCommentCount		8	5	1
    notItemOwnerCommentCount	2	4	1
    
    then the main array will end up with
    userid				1	2	3
    itemOwnerCommentCount		10	6	1
    notItemOwnerCommentCount	6	5	3
    How would I add these values correctly ?



    I have tried...
    Code:
    foreach($tempCounter as $ui => $value) {
    echo("<br>" . $ui . ":<br>:" . $value[$ui] . ":<br><br>");
    $mainCounters[$ui][0] = $mainCounters[$ui][0] + $tempCounters[$ui][0];
    $mainCounters[$ui][1] = $mainCounters[$ui][1] + $tempCounters[$ui][1];
    }
    But think there ay be something wrong with the code that gets the values in the first place that could be causing the array to seem empty. or maybe I have this array sum thing wrong ?
    Last edited by needsomehelp; 12-15-2012 at 12:37 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Can you show me what your array structure looks like from PHP's point of view-- that is to say, run this code:

    PHP Code:
    echo "<pre>".print_r($mainCounters,true)."</pre>"
    And paste the output here. Because, according to how you've described your array, it doesn't make any sense. The "userid" index is unrelated to the counters. So I just want to see exactly what your array looks like before I evaluate the foreach() loop you are using.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    It would be a lot more efficient to use the userid as the key for the array, but based on the example array you gave, here is how you could do it:

    PHP Code:
    <?php

    $tmp 
    = array(    
                    array(    
                            
    'userid' => 1
                            
    ,array(
                                
    'itemOwnerCommentCount' => 2
                                
    ,'notItemOwnerCommentCount' => 4
                            
    )
                    )
                    ,array(    
                            
    'userid' => 2
                            
    ,array(
                                
    'itemOwnerCommentCount' => 1
                                
    ,'notItemOwnerCommentCount' => 1
                            
    )
                    )
                    ,array(    
                            
    'userid' => 3
                            
    ,array(
                                
    'itemOwnerCommentCount' => 0
                                
    ,'notItemOwnerCommentCount' => 2
                            
    )
                    )
                 );

    $main = array(    
                    array(    
                            
    'userid' => 1
                            
    ,array(
                                
    'itemOwnerCommentCount' => 8
                                
    ,'notItemOwnerCommentCount' => 2
                            
    )
                    )
                    ,array(    
                            
    'userid' => 2
                            
    ,array(
                                
    'itemOwnerCommentCount' => 5
                                
    ,'notItemOwnerCommentCount' => 4
                            
    )
                    )
                    ,array(    
                            
    'userid' => 3
                            
    ,array(
                                
    'itemOwnerCommentCount' => 1
                                
    ,'notItemOwnerCommentCount' => 1
                            
    )
                    )
                 );


    foreach( 
    $main as $k => $v )
    {
        foreach( 
    $tmp as $j => $w )
        {
            if( isset( 
    $tmp[$j]['userid'] ) && $tmp[$j]['userid'] == $main[$k]['userid'] )
            {
                
                if( isset( 
    $tmp[$j][0]['itemOwnerCommentCount'] ) )
                    
    $main[$k][0]['itemOwnerCommentCount'] += $tmp[$j][0]['itemOwnerCommentCount'];
                
                if( isset( 
    $tmp[$j][0]['notItemOwnerCommentCount'] ) )
                    
    $main[$k][0]['notItemOwnerCommentCount'] += $tmp[$j][0]['notItemOwnerCommentCount'];
                
                break;
            }
        }
        
    }

    var_export$main ); // outputs :
    /*

    array (
         0 =>      array ( 
                         'userid' => 1,
                         0 => array (
                             'itemOwnerCommentCount' => 10,
                             'notItemOwnerCommentCount' => 6,
                         ),
                     ),
         1 =>     array (
                         'userid' => 2,
                         0 =>  array (
                             'itemOwnerCommentCount' => 6,
                             'notItemOwnerCommentCount' => 5,
                         ),
                     ),
         2 =>      array (
                         'userid' => 3,
                         0 =>  array (
                             'itemOwnerCommentCount' => 1,
                             'notItemOwnerCommentCount' => 3,
                     ),
         ),
    )

    */

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Decent solution but you had to alter needsomehelp's array structure to get there.

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    it turns out that i had a few bugs in the code that was placing values in the array in the first place, causing the arrays not to be added together, also a had in my OP a typo, i have iu instead of ui. seems to be working now.

    Thank you anyway for your help on this.


  •  

    Posting Permissions

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