Reputation: 33
I have created three arrays in a PHP file, all of which got their elements from a MYSQL database. Even though I have used the json_encode method to prepare the arrays, the results is not parsable JSON.
I am very new to JSON and PHP, but I have tried to encode the three arrays into one variable, as well as three different variable. But in both situations when I echo the result, it is not valid JSON.
I created the arrays in the beginning of the PHP file.
$physicianArr = array();
$patientArr = array();
$apmtArr = array();
I used this general method to populate all of the arrays (lines are used for breaking the three examples, not included in code.)
if (mysqli_num_rows($resultPhysician) > 0) {
while($row=mysqli_fetch_assoc($resultPhysician)) {
$physicianArr[] = $row;
}
$physicianJSON = json_encode($physicianArr);
}
if (mysqli_num_rows($resultPatient) > 0) {
while($row=mysqli_fetch_assoc($resultPatient)) {
$patientArr[] = $row;
}
$patientJSON = json_encode($patientArr);
}
if (mysqli_num_rows($resultApmt) > 0) {
while($row=mysqli_fetch_assoc($resultApmt)) {
$apmtArr[] = $row;
}
$apmtJSON = json_encode($apmtArr);
}
RESULTS:
the when I type in the url to retrieve this PHP file on a web browser here is the result, three separate arrays instead of one JSON:
[
{"UserID":"3","FirstName":"Jane","LastName":"Parkey"},
{"UserID":"4","FirstName":"Jamie","LastName":"Crane"},
{"UserID":"5","FirstName":"Jerry","LastName":"Martin"},
{"UserID":"6","FirstName":"Alexander","LastName":"Dollar"},
{"UserID":"8","FirstName":"Bob","LastName":"Loblaw"},
{"UserID":"11","FirstName":"Mary","LastName":"Robbins"},
{"UserID":"15","FirstName":"testy","LastName":"testy"}
][
{"UserID":"1","FirstName":"Joe","LastName":"Smith"},
{"UserID":"2","FirstName":"Adam","LastName":"Stone"},
{"UserID":"9","FirstName":"Michael","LastName":"Jordan"},
{"UserID":"10","FirstName":"Tom","LastName":"Holland"},
{"UserID":"12","FirstName":"test1","LastName":"test1"},
{"UserID":"19","FirstName":"Will","LastName":"Smith"},
{"UserID":"20","FirstName":"Joe","LastName":"Imburgia"}
][
{"apmtID":"1","PhysicianID":"15","apmtDate":"Jun 26, 2019","apmtTime":"09:00:00 AM","PatientID":"1"},
{"apmtID":"2","PhysicianID":"15","apmtDate":"test","apmtTime":"test","PatientID":"1"},
{"apmtID":"4","PhysicianID":"15","apmtDate":"Apr 20, 2019","apmtTime":"7:10:36 AM","PatientID":"1"}
]
Upvotes: 2
Views: 178
Reputation: 94642
Here is a suggestion. If you want one data structure then only echo one.
// make sure all arrays exist
$physicianArr = [];
$patientArr = [];
$apmtArr = [];
if (mysqli_num_rows($resultPhysician) > 0) {
while($row=mysqli_fetch_assoc($resultPhysician)) {
$physicianArr[] = $row;
}
}
if (mysqli_num_rows($resultPatient) > 0) {
while($row=mysqli_fetch_assoc($resultPatient)) {
$patientArr[] = $row;
}
}
if (mysqli_num_rows($resultApmt) > 0) {
while($row=mysqli_fetch_assoc($resultApmt)) {
$apmtArr[] = $row;
}
}
echo json_encode(
[
'Physicians' => $physicianArr
'Patients' => $patientArr
'Ampt' => $apmtArr
]
);
Upvotes: 2
Reputation: 490
you can use the function array_merge so that your arrays are combined into one. once you do this, you can use json_encode function to encode this one array that holds all the data from the three arrays.
Upvotes: 0