Array: how to combine similar values by key

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shishi
Forum Newbie
Posts: 14
Joined: Wed Apr 22, 2009 12:05 am

Array: how to combine similar values by key

Post by shishi »

hello. i hope someone can help me with this....

i have a 2-dimensional array with the ff values:
the persons names:

Code: Select all

$array[0]['name']='Tom';
$array[1]['name']='Jerry';
$array[2]['name']='Tom';
$array[3]['name']='John';
$array[4]['name']='Mike';
each person's age

Code: Select all

$array[0]['age']=10;
$array[1]['age']=12;
$array[2]['age']=12;
$array[3]['age']=15;
$array[4]['age']=13;
i need to add the ages of the persons with the same name.

here's the result i need:

Code: Select all

$array[0]['name']='Tom';
$array[0]['age']=22;
 
$array[1]['name']='Jerry';
$array[1]['age']=12;
 
$array[2]['name']='John';
$array[2]['age']=15;
 
$array[3]['name']='Mike';
$array[3]['age']=13;
how do i do this in PHP? is there a built-in function that i can use? pls advise/help
Last edited by shishi on Sat May 09, 2009 4:48 am, edited 2 times in total.
Jody LeCompte
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 12:01 am

Re: Array: how to combine similar values by key

Post by Jody LeCompte »

Are you wanting a method to continue to add and sort information on the fly or something that would be manually updated? The code you've supplied really already does what you need it to.

print_r of $array:

Code: Select all

 
Array ( [0] => Array ( [name] => Tom [age] => 10 ) [1] => Array ( [name] => Jerry [age] => 12 ) [2] => Array ( [name] => Tom [age] => 12 ) [3] => Array ( [name] => John [age] => 15 ) [4] => Array ( [name] => Mike [age] => 13 ) )
shishi
Forum Newbie
Posts: 14
Joined: Wed Apr 22, 2009 12:05 am

Re: Array: how to combine similar values by key

Post by shishi »

i need a method/code that will group similar values by key (in this case by 'name') pls
Jody LeCompte
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 12:01 am

Re: Array: how to combine similar values by key

Post by Jody LeCompte »

What are you trying to do in the big picture that the code you already have doesn't satisfy?

I'm having trouble understanding exactly what you're asking, because as I said, your code already groups it exactly how you said you want it.
shishi
Forum Newbie
Posts: 14
Joined: Wed Apr 22, 2009 12:05 am

Re: Array: how to combine similar values by key

Post by shishi »

i have only written my expected output. i dnt know exactly how to code it.
for example i have this array:

Code: Select all

$array[0]['name']='Tom';
$array[1]['name']='Jerry';
$array[2]['name']='Tom';
$array[3]['name']='John';
$array[4]['name']='Mike';
 
$array[0]['age']=10;
$array[1]['age']=12;
$array[2]['age']=12;
$array[3]['age']=15;
$array[4]['age']=13;
there are two persons of the same name which is Tom. The 1st Tom's age is 10 and the 2nd Tom's age is 12.
how will i code it such that the resulting array will be:

Code: Select all

$array[0]['name']='Tom';
$array[0]['age']=22;  //10+12
 
$array[1]['name']='Jerry';
$array[1]['age']=12;
 
$array[2]['name']='John';
$array[2]['age']=15;
 
$array[3]['name']='Mike';
$array[3]['age']=13;
the array size is reduced by 1 since Tom's entries are already combined.

im sorry if i can't express it clearly as English isnt my primary language. but i hope the example input/output helps
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Array: how to combine similar values by key

Post by Mark Baker »

Code: Select all

 
$newArray = array();
foreach ($array as $entry) {
   $name = entry['name'];
   $age = entry['age'];
   if (isset($newArray[$name])) {
      $newArray[$name] += $age;
   } else {
      $newArray[$name] = $age;
   }
}
print_r($newArray);
 
shishi
Forum Newbie
Posts: 14
Joined: Wed Apr 22, 2009 12:05 am

Re: Array: how to combine similar values by key

Post by shishi »

hi sir! thank you so much
Post Reply