我正在尝试创建体育运动员的JSON响应。我有这个脚本,它查询MySQL数据库并生成一个关联数组。
$meta = array();
while($res = mysqli_fetch_assoc($query)) {
// $meta[] = $res;
$meta[] = array(
'guid' => $res['guid'],
'name' => $res['name'],
'dob' => $res['date_of_birth'],
'birthplace' => $res['birthplace'],
'height' => $res['height'],
'weight' => $res['weight'],
'position' => $res['position'],
'honours' => $res['honours']
);
}
$meta = json_encode(array('players' => $meta), JSON_PRETTY_PRINT);
echo $meta
我希望能够为数据库中的每个播放器返回一个"播放器"JSON对象,但我不确定如何创建该结构。
这是我目前的回应:
{
"players": [
{
"guid": "1",
"name": "Matias Aguero",
"dob": "1981-02-13",
"birthplace": "San Nicolas, Argentina",
"height": "1.83m (6' 0'")",
"weight": "109kg (17st 2lb)",
"position": "Prop",
"honours": "40 caps"
},
{
"guid": "2",
"name": "George Catchpole",
"dob": "1994-02-22",
"birthplace": "Norwich, England",
"height": "1.85em (6ft 1'")",
"weight": "104kg (16st 5lb)",
"position": "Centre",
"honours": ""
},
{
"guid": "3",
"name": "Logovi'i Mulipola",
"dob": "1987-03-11",
"birthplace": "Manono, W Samoa",
"height": "1.93 (6' 4'")",
"weight": "130kg (20st 6lb)",
"position": "Prop",
"honours": "Samoa (17 caps)"
}
]
}
我的理想架构是
"players" : [ "player" { "data" } ] // APOLOGIES FOR THE SHORTHAND
$meta = array();
while($res = mysqli_fetch_assoc($query)) {
// $meta[] = $res;
$meta[$res['name']] = array(
'guid' => $res['guid'],
// 'name' => $res['name'],
'dob' => $res['date_of_birth'],
'birthplace' => $res['birthplace'],
'height' => $res['height'],
'weight' => $res['weight'],
'position' => $res['position'],
'honours' => $res['honours']
);
}
$meta = json_encode(array('players' => $meta), JSON_PRETTY_PRINT);
echo $meta
如果我不想询问