Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON to print empty array.

I'm new to php and not sure why the go-daddy server treats JSON data differently. When I host it locally on my computer in local host, when I want to echo empty JSON array, I simply put [] shown as below. But when I uploaded the code to go-daddy server and try it out it echoed an error, Parse error: syntax error, unexpected '[', expecting ')' in...I'm wondering how can I put the JSON so it can echo [] when needed. Otherwise it will give "null" and when it parse into AS3, it turn into a JSON parse error.

if (!empty($output)){
        echo json_encode( $output );}
        else{
            echo json_encode( [] );
        }
like image 960
Benyaman Avatar asked Apr 14 '14 05:04

Benyaman


People also ask

Can JSON have empty array?

JSON data has the concept of null and empty arrays and objects.

Is empty string a valid JSON?

An empty string is not a valid json, then it fails.

How does JSON handle null values?

If you want to represent a null value in JSON, the entire JSON string (excluding the quotes containing the JSON string) is simply null . No braces, no brackets, no quotes.

How check JSON array is empty or not in C#?

IsSuccessStatusCode && instituteDetails. Length>2) and if(createModel. Count()>0) worked fine if you getting empty jsonstring and model .


1 Answers

That is because your webserver version must be less than 5.4. You are trying to use a new feature of PHP 5.4 called the short array syntax

Use echo json_encode(array()); instead of echo json_encode( [] );

Working Demo on PHP v 5.3

like image 98
Shankar Narayana Damodaran Avatar answered Oct 02 '22 15:10

Shankar Narayana Damodaran