Skip to content
Advertisement

Json encode an entire mysql result set

I want to get json with php encode function like the following

JavaScript

i want the output json to be something like that

JavaScript

but the output json is look like the following

JavaScript

As you realize that there is comma at the end, i want to remove it so it can be right json syntax, if i removed the echo ","; when there’s more than one result the json will generate like this {"results":[{"id":1,"name":"name1"}{"id":2,"name":"name2"}]} and that syntax is wrong too

Hope that everybody got what i mean here, any ideas would be appreciated

Advertisement

Answer

If I were you, I would not json_encode each individual array, but merge the arrays together and then json_encode the merged array at the end. Below is an example using 5.4’s short array syntax:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement