我正在尝试在从MySQL提取数据之前添加其他信息,以便我可以json格式输出它,但是,我不确定如何在php中获取所需的结构。
我似乎无法用实际的数据字符串(即RoomID = 1)而不是(值0 = 1)替换json值(0,1,3,4),然后数组结构是完全的,并且不能修正它。这是我到目前为止尝试过的。
<?php
$floorID = $_GET['FloorID'];
$roomID = '';
$roomName = '';
$description ='';
$image = '';
if ($floorID != '')
{
$conn = mysql_connect($servername, $db_username, $db_password, $dbname);
mysql_select_db($dbname);
$query = "SELECT * FROM `Apartments` WHERE FloorID = '$floorID'";
if (!$result = mysql_query($query))
{
die("Error: " . mysql_error($con));
}
$data = array(
'message' => 'Your message here',
'value' => array()
);
while($r = mysql_fetch_assoc($result)) {
$data['value'][] = array(
$roomID = $r['RoomID'],
$roomName = $r['RoomName'],
$description = $r['Description'],
$image = $r['Image'],
);
}
echo json_encode($data);
mysql_close($conn);
}
?>
这是我得到的json ..
{
"message": "Your message here",
"value": [
[
"1",
"Coffee",
"This is the great coffee session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"4 hours"
],
[
"1",
"Tea",
"This is the great tea session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hours"
],
[
"1",
"Herb",
"This is the great Herb session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"1 hour"
],
[
"2",
"mechanic",
"This is the great mechanic session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
],
[
"2",
"navigator",
"This is the great navigator session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
],
[
"2",
"detective",
"This is the great detective session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
],
[
"3",
"housekeeper",
"This is the great housekeeper session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
],
[
"3",
"piano",
"This is the great piano session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
],
[
"3",
"piano",
"This is the great piano session",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/A_small_cup_of_coffee.JPG/1200px-A_small_cup_of_coffee.JPG",
"Public",
"2 hour"
]
]
}
但这是我想要获得的Json结构。
[
{
"FloorID" : 1,
"ApartmentID" : 1,
"AllRooms" : [
{
"RoomID" : 1,
"RoomName" : "room One",
"duration" : "",
"image" : "https://image.flaticon.com/icons/png/512/1458/1458512.png"
},
{
"RoomID" : 2,
"RoomName" : "room Two",
"duration" : "",
"image" : "https://i.pinimg.com/originals/62/1a/b7/621ab74618686de0f2a3bed8b4080e90.png"
},
{
"RoomID" : 3,
"RoomName" : "room Three",
"duration" : "",
"image" : "https://visiblehr.com/wp-content/uploads/2019/09/6.png"
},
{
"RoomID" : 4,
"RoomName" : "room Four",
"duration" : "",
"image" : "https://image.flaticon.com/icons/png/512/1458/1458512.png"
}
]
},
{
"FloorID" : 1,
"ApartmentID" : 2,
"AllRooms" : [
{
"RoomID" : 5,
"RoomName" : "room two one",
"duration" : "",
"image" : "https://image.flaticon.com/icons/png/512/1458/1458512.png"
},
{
"RoomID" : 6,
"RoomName" : "Session two Two",
"duration" : "",
"image" : "https://image.flaticon.com/icons/png/512/1458/1458512.png"
}
]
}
]
如您所见,我的输出未显示值的名称,而是保留为空?有人可以引导我进入正确的道路吗?我是json和php的新手,谢谢。