Using PHP multidimensional arrays to convert MySQL to JSON

前端 未结 4 1826
北荒
北荒 2020-12-12 03:07

Here\'s my table structure.

I\'m trying to convert MySQL to nested JSON, but am having trouble figuring out how to build the multidimensional array in PHP.

T

4条回答
  •  不知归路
    2020-12-12 03:38

    Change your while to this:

    while ($row = mysqli_fetch_assoc($fetch)) {
        $row_array[$row['school_name']]['school_name'] = $row['school_name'];
        $row_array[$row['school_name']]['terms']['term_name'] = $row['term_name'];
        $row_array[$row['school_name']]['terms']['department_name'][] = array(
            'department_name' => $row['department_name'],
            'department_code' => $row['department_code']
        );
    }
    

    Edit

    If you want to achieve result like the example, maybe you should consider using this method:

    Probably it's not an effective program, but it should gives you best result. Hope it helps :)

提交回复
热议问题