Skip to content
Advertisement

Show two MySQL column as key and value in JSON format

I want to show this MySQL table data in JSON object format with _key column as key and _value column as value. mysql table data

How can I do this with PDO in PHP?

I tried like this:

JavaScript

But this gives me like this:

JavaScript

But I want to get result like this:

JavaScript

Advertisement

Answer

JavaScript

Output:

JavaScript

I don’t know why would you want to put a single object in a JSON array. My solution outputs just the object. If you want to wrap it in an array then just put it inside of [] before sending to json_encode()

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