hello. i hope someone can help me with this.... i have a 2-dimensional array with the ff values: the persons names: $array[0]['name']='Tom'; $array[1]['name']='Jerry'; $array[2]['name']='Tom'; $array[3]['name']='John'; $array[4]['name']='Mike'; each person's age $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. 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: $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; the array size is reduced by 1 since Tom's entries are already combined. pls help.
Try using classes; class people { var $name, $age; //constructor function people(){} } $array[0] = new people; $array[0]->name = "Tom"; $array[0]->age = 10; // etc PHP: You then need to build an iterative function to check each name against the rest of the array. Alternatively you could add each 1 to a datbase and do a check to see if the name exists each time you add a record. (easiest method in my opinion). Or... you could create a new array with the name as the key and loop the array adding the age to the total age for that array; e.g. foreach($array as $key => $value){ $newarray[$value->name] = $newarray[$value->name] + $value->age; } PHP: