Page 1 of 1
Array: how to combine similar values by key
Posted: Sat May 09, 2009 12:15 am
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
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 12:24 am
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 ) )
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 12:30 am
by shishi
i need a method/code that will group similar values by key (in this case by 'name') pls
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 12:42 am
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.
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 12:50 am
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
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 4:20 am
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);
Re: Array: how to combine similar values by key
Posted: Sat May 09, 2009 4:28 am
by shishi
hi sir! thank you so much