I have a json response how do i echo the response in message
i tried json_encode
but it seems not working
{
"success": "verification_error",
"message": [
"required.",
]
}
I have a json response how do i echo the response in message
i tried json_encode
but it seems not working
{
"success": "verification_error",
"message": [
"required.",
]
}
I think you mix json_encode
and json_decode
.
json_encode
is used to create a json
object (from an array for example).
json_decode
do the opposite: decode a json
object.
Another point is that the message
field is an array. You can not directly display array
as string
.
In your example, you only have one element in the array. So you can directly display it by selecting it (with index).
If the array has more than one element, you can either iterate over the array and apply the first case or implode
the array to a string. Some discussion about the question here.
Here an example:
<?php
$a = array("success"=> "verification_error",
"message"=> [
"required.",
]
);
$json_response = json_encode($a);
echo("Your response (json encoded): ".$json_response);
// Your response (json encoded): {"success":"verification_error","message":["required."]}
$data = json_decode($json_response, true);
echo("<br/>Message: ".$data["message"][0]);
// Message: required.
?>