循环使用A-Z并使用MYSQL将数组排序为JSON

时间:2016-11-10 21:39:29

标签: php arrays json multidimensional-array

使用JSON的阵列......我的敌人。

我试图让它正确地对A-Z进行排序,我将解释。我有以下PHP代码:

$az_result = $con->query("SELECT * FROM games ORDER BY game_title");

while($az_row = mysqli_fetch_array($az_result))
{
    $the_az_game_data = array();

    $empty_letter = '';

    $current_letter = strtolower($az_row['game_title'][0]);
    if ($empty_letter != $current_letter) {
        $cur_letter = strtoupper($current_letter);
        $empty_letter = $current_letter;
    }

    $game_id = $az_row['ID'];

    $the_az_game_data['game_id'] = $game_id;
    $the_az_game_data['game_name'] = $az_row['game_title'];
    $the_az_game_data['game_link'] = $az_row['game_link'];


    //store data
    $az_data[$cur_letter] = $the_az_game_data;

}

$game_data[] = $az_data;


echo '{"games":'.json_encode($game_data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).'}';

以下是JSON输出:

{"games":[
    {
        "C": {
            "game_id": "17",
            "game_name": "Cool Game",
            "game_link": "cool-game",
        },
        "R": {
            "game_id": "2",
            "game_name": "Rad Game 2",
            "game_link": "rad-game-2",
        },
        "T": {
            "game_id": "1",
            "game_name": "Test Game",
            "game_link": "test-game",
        }
    }
]}

所以我几乎拥有它,但错误的是这些字母中实际上有多个游戏。它是重载数组还是什么?有人可以解释一下,谢谢!

0 个答案:

没有答案