我是php的新手,获取存储json的mysql结果,但它不是我想要的gettng coorect格式。
检查以下代码
$sql = "select * from en_providers where providerEmailAddress='" . $email . "' and providerPW='" . $password . "'";
$result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($connection));
if (mysqli_num_rows($result) > 0) {
$resultArray = array();
while ($row = mysqli_fetch_assoc($result)) {
$providerID = $row['providerID'];
$resultArray['providers'] = $row;
$resultArray['providers']['providerIDActivities'] = unserialize($row['providerIDActivities']);
$resultArray['providers']['providerIDBodies'] = unserialize($row['providerIDBodies']);
$resultArray['providers']['providerIDOthers'] = unserialize($row['providerIDOthers']);
$sql1 = "select * from en_venues where providerID = $providerID ";
$result1 = mysqli_query($con, $sql1) or die("Error in Selecting " . mysqli_error($connection));
$i = $j = $l = $x = $m = 0;
while ($row1[] = mysqli_fetch_assoc($result1)) {
//$resultArray['venues'][]['venueIDFacilities'] = unserialize($row1[$j++]['venueIDFacilities']);
$venueID = $row1[$j++]['venueID'];
$k = 0;
$venueFacilities = unserialize($row1[$i++]['venueIDFacilities']);
$resultArray[$x++]['venues']['venueIDFacilities'] = $venueFacilities;
//$resultArray['venues'][$x++]['venueID'] = $venueID;
$resultArray['venues'] = $row1;
//echo json_encode($resultArray);
echo json_encode($resultArray);
}
}
输出是:
{
0: {
"venues": {
"venueIDFacilities": [
"1",
"2",
"3"
],
}
},
1: {
"venues": {
"venueIDFacilities": [
"4",
"7"
],
}
}
},
"providers": {
"providerIDActivities": [
"218",
"219"
],
"providerIDSports": "a:1:{i:0;i:82;}",
"providerIDBodies": [
"112"
],
},
venues": {
0: {
"venueID": "9",
"providerID": "2"
},
1: {
"venueID": "238",
"providerID": "2",
"venueActive": "yes"
}
}
但是我需要那些VenueFailities
在各自的场地,但结果却在外面。如何将这些值附加到场地?
我想用不同的方式尝试一天,但是格式不正确。
我想要的输出:
"providers": {
"providerIDActivities": [
"218",
"219"
],
"providerIDSports": "a:1:{i:0;i:82;}",
"providerIDBodies": [
"112"
],
},
venues": {
0: {
"venueID": "9",
"providerID": "2",
"venueIDFacilities": [
"4",
"7"
]
},
1: {
"venueID": "238",
"providerID": "2",
venueIDFacilities": [
"4",
"7"
]
}
}
答案 0 :(得分:1)
这一行
$resultArray[$x++]['venues']['venueIDFacilities'] = $venueFacilities;
应该是
$resultArray['venues'][$x++]['venueIDFacilities'] = $venueFacilities;