Below code is the returned JSON
[{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}]
Below code is the desired result of the returned JSON (without the array bracket)
{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}
Below code is to convert the array to the JSON format
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail");
$specific = [];
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="100px">' . $row['id'] . '</td>';
echo '<td width="200px">' . $row['patientid'] . '</td>';
echo '<td width="200px">' . $row['vaccineid'] . '</td>';
//**********Convert the array into json*******************
$specific[] = ["one" => $row["id"],
"two" => $row["patientid"],
"three" => $row["vaccineid"]];
$result = json_encode($specific,JSON_UNESCAPED_UNICODE);
echo $result;
echo "</tr>";
echo "</table>";
?>
To send the request to the API, iam using Guzzle And the format the API require is {xx:xx},{xx:xx} without the square bracket, any idea how to remove it using PHP. Thanks in advance
$client = new Client([
'headers' => ['Content-Type' => 'application/json',
'Token' => $token]
]);
$response = $client->post('http://localhost:91/Religious',
['body' => ***Where the json will be place***]
);
I read a nice solution in the comments of the first post by deceze ♦ with trim()
.
$yourJson = trim($yourJson, '[]');
You can also use regular expression:
// if nothing is found, your json has already no brackets or is invalid.
if (preg_match('/^\[(.+)\]$/', $yourJson, $new))
{
/**
* $new[0] = $yourJson
* $new[1] = what's in the parenthesis
*/
$yourJson = $new[1];
}
Or, you may use substr()
:
$yourJson = substr($yourJson, 1, strlen($yourJson) - 2);
EDIT:
When it says in the request body format : application/json
, I do not think that you have to remove the brackets. Did you even try with them?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With