-3

我有一个表'目标':

    ID   TYPE   KEY     VALUE
    --   ----   ---     -----
    1    RED    BASE    3
    2    BLUE   NAME    ALLOR
    3    RED    MAIN    _TTR
    4    GREEN  LOCAL   PIN,SEC,BALL,UNI
    5    BLUE   ALT     2DFFRST34@HH&FR#
    6    GREEN  DOMAIN  SITE.ORG,NSPL.EDU,ROAR.IN

我想在 PHP 中创建一个多维数组,它将生成这个数组:

    $GOAL = array (
        'RED'  => array    (
               'BASE' => 3,
               'MAIN' => '_TTR'
        ),
        'BLUE'   => array(
               'NAME' => 'ALLOR',
               'ALT' => '2DFFRST34@HH&FR#'
        ),
        'GREEN'   => array(
               'LOCAL' => 'PIN,SEC,BALL,UNI',
               'DOMAIN' => 'SITE.ORG,NSPL.EDU,ROAR.IN'
        )
    );

查询应该是什么?

4

1 回答 1

0

像这样的东西应该工作

  <?php 
        $query = mysql_query('SELECT * FROM GOAL');
        $goal = array();
        while($row = mysql_fetch_object($query)) {
            if(!isset($goal[$row->type])) {
                $goal[$row->type] = array();
            }
            $goal[$row->type][$row->key] = $row->value;
        }
    ?>
于 2013-09-11T18:00:28.680 回答