How can I get rid of the brackets below for json processing?
[{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}]
The result above is processed by the class below into an array,
function handle_upload($upload_directory)
{
# Loop the code according to the number of files.
for($i = 1; $i <= $this->total; $i++)
{
...
if ($this->file->save($upload_directory.$name_filtered.'.'.$file_extension , $i-1))
{
$message[] = array('success'=>true,'filename'=>$name_filtered.'.'.$file_extension);
}
else
{
$message[] = array('error'=> 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
}
}
return $message;
}
Then I use json_encode
to turn the array into json format,
$uploader = new uploader();
$result = $uploader->handle_upload('uploads/');
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
But I only need this in my result without the brackets,
{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}
' { } ' used for Object and ' [] ' is used for Array in json.
Curly brackets {} delimit the beginning and end of a JSON object, therefore they are always found as the very first and last lines of a request. A JSON object contains one or more key-value pairs, where each pair is separated from the next by a comma, except for the last pair.
Is {} a valid JSON? At the time of writing, JSON was solely described in RFC4627. It describes (at the start of “2”) a JSON text as being a serialized object or array. This means that only {} and [] are valid, complete JSON strings in parsers and stringifiers which adhere to that standard.
JSON Syntax Rules Curly braces organize objects, and the colon isolates each name. Square brackets hold the array, and commas separate values.
str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));
?
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