0

如何正确分组数组:(最终结果)

Array
(
    [0] => Array
        (
            [Player] => CaLvErT
            [0] => Array
                (
                    [SnapTime] => 1330028992
                    [PlayCount] => 9
                )
        )
    [1] => Array
        (
            [Player] => CaLvErT
            [0] => Array
                (
                    [SnapTime] => 1330202828
                    [PlayCount] => 8
                )
        )
)

我的 mySQL/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)){
           $iTestGrid[] = array(
            'Player' => $FinalRow['player'],
            array(
                'SnapTime' => (int)$FinalRow['snap'],
                'PlayCount' => (int)$FinalRow['pCnt']
            )
           );
        }

基本上想知道如何对同名的玩家进行分组。

在此先感谢您的帮助!(请原谅我,因为我并不热衷于完全理解数组是如何工作的,整天/晚上都在书店里,但仍然得到它的一部分,而不是很多)

4

2 回答 2

2

如果我理解正确,你想要这样的东西。您可以使用播放器名称作为分组键,并将 playcount/timestamp 值推送到同一个数组中。

<?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);
于 2012-05-05T06:50:46.940 回答
0

也许您可以使用player names 作为数组键并在该数组中插入其他数据:

$sorted = array();
foreach ( $result as $value ) {
    $sorted[ $value['Player'] ] = $value[ 0 ];
}
var_dump( $sorted );
于 2012-05-05T07:02:56.603 回答