nuttycoder Posted February 5, 2010 Share Posted February 5, 2010 Hi guys, I've been trying to get values from some arrays and group them it's kinda hard to explain let me give you an example I have this output: Richard Whiting - 65 Ewan Dowes - 43 Reece Lyne - 34 Kirk Yeaman - 32 Kirk Yeaman - 27 Willie Manu - 16 Mark Calderwood - 13 Ewan Dowes - 2 there are duplicates in the names I need to remove the duplicate name and keep the number but group them so they end up like this: Richard Whiting - 65 Ewan Dowes - 2,43 Reece Lyne - 34 Kirk Yeaman - 27,32 Willie Manu - 16 Mark Calderwood - 13 this is the code I've used to get this far: inside a loop I add the data to an array: $tsname[] = array($r['Team2TeamSheetPersonName'] => $timeLine->Time); $key is the name and $value is the number foreach ($tsname as $key => $value) { foreach ($value as $key => $value) { echo "<p>$key - $value</p>"; } } I've tried everything I can think of and am stuck not sure how to do this, if anyone can provide any help on this I would be very greatful. thanks in advance. Link to comment https://forums.phpfreaks.com/topic/191033-grouping-with-arrays/ Share on other sites More sharing options...
Wolphie Posted February 5, 2010 Share Posted February 5, 2010 The quickest and easiest way I can see is <?php $array['a'][0] = 3; $array['a'][1] = 1; $array['b'][0] = 2; $array['b'][1] = 1; $array['c'][0] = 0; $array['c'][1] = 1; foreach (array_keys($array) as $key) { $array[$key] = array_sum($array[$key]); } print_r($array); ?> Array ( [a] => 4 [b] => 3 [c] => 1 ) Link to comment https://forums.phpfreaks.com/topic/191033-grouping-with-arrays/#findComment-1007344 Share on other sites More sharing options...
RussellReal Posted February 5, 2010 Share Posted February 5, 2010 foreach ($tsname as $key => $value) { foreach ($value as $key => $value) { echo "<p>$key - $value</p>"; } } firstly your code looks like it shouldn't work secondly, I don't understand how you can have more than 1 name as a key.. in the same array (probably came from another dimension of the first array. thirdly how I would do it, is like this: <?php $overall = array(); foreach ($tsname as $v) { foreach ($v as $key => $value) { $overall[$key] = ((array_key_exists($key,$overall))? $overall[$key].',':'').$value; } } print_r($overall); ?> Link to comment https://forums.phpfreaks.com/topic/191033-grouping-with-arrays/#findComment-1007348 Share on other sites More sharing options...
nuttycoder Posted February 5, 2010 Author Share Posted February 5, 2010 that's brilliant works great I agree with your points, I didn't think having more then one name as key would work either I got to the point where anything was worth a try. I don't tend to use array beyond doing basic things. here's the output now: Array ( [Richard Whiting] => 65 [Ewan Dowes] => 43,2 [Reece Lyne] => 34 [Kirk Yeaman] => 32,27 [Willie Manu] => 16 [Mark Calderwood] => 13 ) what would your recommend is the best way to print out this data so it's like: Richard Whiting 65 Ewan Dowes 43,2 Reece Lyne 34 Kirk Yeaman 32,27 Willie Manu 16 Mark Calderwood 13 thanks very much Link to comment https://forums.phpfreaks.com/topic/191033-grouping-with-arrays/#findComment-1007386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.