Problems with php using mysql query

ThinkPhp query the following content
Game game_zone;
the structure of the table is
zone_name-- Theater name
gid-- Game ID

want to query a returned json in the following format:

"1": 
[
    {"name": "", "id": "1"},
    {"name": "", "id": "2"},
],
"2":
[
    {"name": "", "id": "3"},
    {"name": "", "id": "4"},
],

wonder how to query? There is no way to use dynamic associative preloading and not thinking about subqueries. The most important thing is: the index of the array is the ID, of the game. Or is the data in this format written manually, not queried and converted to json?

the gods who help solve the problem must be rewarded!

Mar.18,2021

consider that it is unlikely that a stored procedure wants to solve with a sql.


query first, and then loop through the results, using id as the index.


fetch the data first, and then array_walk process


should be written manually, query it out and then use php for processing.

public function test()
{
    $list = Db::name('zone_name')->select();
    $res = [];
    foreach ($list as $k => $v) {
        $res[$v['gid']][] = $v;
    }
    return $res;
}

Why do database queries return results that must meet your format requirements?
this in itself is the function that needs to be implemented in the business layer of your code!

Menu