Reputation: 327
How do I properly group an array: (end result)
Array
(
[0] => Array
(
[Player] => CaLvErT
[0] => Array
(
[SnapTime] => 1330028992
[PlayCount] => 9
)
)
[1] => Array
(
[Player] => CaLvErT
[0] => Array
(
[SnapTime] => 1330202828
[PlayCount] => 8
)
)
)
My mySQL/PHP goes with what I tried:
$iTestGrid = array();
$ChartSQL = "SELECT player,snap,count(*) AS pCnt";
$ChartSQL .= " FROM sc".$DBSvr;
$ChartSQL .= " WHERE hex(alliance) IN (hex('Twitch'))";
$ChartSQL .= " GROUP BY player,snap";
$ChartSQL .= " ORDER BY player ASC,snap ASC";
$FinalResult = $db-> query($ChartSQL);
while ($FinalRow = $db-> fetch_assoc($FinalResult)){
$iTestGrid[] = array(
'Player' => $FinalRow['player'],
array(
'SnapTime' => (int)$FinalRow['snap'],
'PlayCount' => (int)$FinalRow['pCnt']
)
);
}
Basically wanted to know how to group a player, that has the same name.
Thanks in advance for your help! (Forgive me for I'm not keen on understanding fully how arrays work, been thru bookstore all day/night and still get parts of it, not a whole lot)
Upvotes: 0
Views: 335
Reputation: 4868
Maybe you could use player name
s as array keys and insert other data inside that array:
$sorted = array();
foreach ( $result as $value ) {
$sorted[ $value['Player'] ] = $value[ 0 ];
}
var_dump( $sorted );
Upvotes: 0
Reputation: 3962
You want something like that if I understand correctly. You can use the player name as key for grouping, and push playcount/timestamp values into the same array.
<?php
$iTestGrid = array();
$ChartSQL = "SELECT player,snap,count(*) AS pCnt";
$ChartSQL .= " FROM sc".$DBSvr;
$ChartSQL .= " WHERE hex(alliance) IN (hex('Twitch'))";
$ChartSQL .= " GROUP BY player,snap";
$ChartSQL .= " ORDER BY player ASC,snap ASC";
$FinalResult = $db-> query($ChartSQL);
while ($FinalRow = $db-> fetch_assoc($FinalResult)){
if(!isset($iTestGrid[$FinalRow['player']]))
$iTestGrid[$FinalRow['player']] = array();
$iTestGrid[$FinalRow['player']][] =
array(
'SnapTime' => (int)$FinalRow['snap'],
'PlayCount' => (int)$FinalRow['pCnt']
);
}
var_dump($iTestGrid);
Upvotes: 2