Jump to content

grouping with arrays


nuttycoder

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.