Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2012, 09:56 AM   PM User | #1
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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..
needsomehelp is offline   Reply With Quote
Old 12-15-2012, 10:54 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Old 12-16-2012, 05:44 AM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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,
                 ),
     ),
)

*/
Inigoesdr is offline   Reply With Quote
Old 12-16-2012, 06:01 AM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Decent solution but you had to alter needsomehelp's array structure to get there.
__________________
Fumigator is offline   Reply With Quote
Old 12-16-2012, 09:57 AM   PM User | #5
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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.
needsomehelp is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:09 AM.


Advertisement
Log in to turn off these ads.