我正在尝试将数据转换为 json,我可以对输出进行硬编码,但需要使用 MySQL 数据和一些代码动态生成代码来计算结果。json 输出将采用以下格式:
{
"inits": {
"version": "18.05.04_EP1",
"source": "Live",
"lowid": "265067",
"highid": "265068",
"ql": "300",
"name": "Ofab Shark Mk 1",
"inits": [
{
"-init": "430",
"-percent": "100",
"-slider": "DEF>===========][<AGG"
},
{
"-init": "530",
"-percent": "90",
"-slider": "DEF>==========][=<AGG"
},
{
"-init": "630",
"-percent": "81",
"-slider": "DEF>=========][==<AGG"
},
{
"-init": "730",
"-percent": "72",
"-slider": "DEF>========][===<AGG"
},
{
"-init": "830",
"-percent": "63",
"-slider": "DEF>=======][====<AGG"
},
{
"-init": "930",
"-percent": "54",
"-slider": "DEF>======][=====<AGG"
},
{
"-init": "1030",
"-percent": "45",
"-slider": "DEF>=====][======<AGG"
},
{
"-init": "1130",
"-percent": "36",
"-slider": "DEF>====][=======<AGG"
},
{
"-init": "1290",
"-percent": "27",
"-slider": "DEF>===][========<AGG"
},
{
"-init": "1590",
"-percent": "18",
"-slider": "DEF>==][=========<AGG"
},
{
"-init": "1890",
"-percent": "9",
"-slider": "DEF>=][==========<AGG"
},
{
"-init": "2190",
"-percent": "0",
"-slider": "DEF>][===========<AGG"
}
]
} }
"inits": [init, percent, slider] 需要在循环内才能生成结果。我可以通过使用静态地做到这一点:
array (
'inits' =>
array (
'version' => '18.05.04_EP1',
'source' => 'Live',
'lowid' => '265067',
'highid' => '265068',
'ql' => '300',
'name' => 'Ofab Shark Mk 1',
'inits' =>
array (
0 =>
array (
'-init' => '430',
'-percent' => '100',
'-slider' => 'DEF>===========][<AGG',
),
1 =>
array (
'-init' => '530',
'-percent' => '90',
'-slider' => 'DEF>==========][=<AGG',
),
2 =>
array (
'-init' => '630',
'-percent' => '81',
'-slider' => 'DEF>=========][==<AGG',
),
3 =>
array (
'-init' => '730',
'-percent' => '72',
'-slider' => 'DEF>========][===<AGG',
),
4 =>
array (
'-init' => '830',
'-percent' => '63',
'-slider' => 'DEF>=======][====<AGG',
),
5 =>
array (
'-init' => '930',
'-percent' => '54',
'-slider' => 'DEF>======][=====<AGG',
),
6 =>
array (
'-init' => '1030',
'-percent' => '45',
'-slider' => 'DEF>=====][======<AGG',
),
7 =>
array (
'-init' => '1130',
'-percent' => '36',
'-slider' => 'DEF>====][=======<AGG',
),
8 =>
array (
'-init' => '1290',
'-percent' => '27',
'-slider' => 'DEF>===][========<AGG',
),
9 =>
array (
'-init' => '1590',
'-percent' => '18',
'-slider' => 'DEF>==][=========<AGG',
),
10 =>
array (
'-init' => '1890',
'-percent' => '9',
'-slider' => 'DEF>=][==========<AGG',
),
11 =>
array (
'-init' => '2190',
'-percent' => '0',
'-slider' => 'DEF>][===========<AGG',
),
),
), )
但是,我不知道如何调用任何方法来填充每个数组(init、percent、slider)的数组数据。我是 PHP 新手,很难找到 PHP 的正确用法,我是 ac# 开发人员。如果有人可以提供帮助,我将不胜感激!