Skip to content
Advertisement

PHP PDO output to specific Json Format

I have been trying to get a query output formatted in a specific way but I am surely doing something wrong. Could anyone that knows what I am doing wrong give me a hand? Thanks a lot.

I have the following db call:

JavaScript

If I run this query in the db (mysql) I get this:

JavaScript

When I make a call to the DB in PHP using PDO I do the following:

JavaScript

My echo inside the loop shows this:

JavaScript

But what I need now is to create a variable ($dtp) outside the while loop that looks like this:

JavaScript

Any ideas on the best way to do this? Thanks again in advance.

Advertisement

Answer

Just use

JavaScript

and you get an array like

JavaScript

You want to create such an array:

JavaScript

It only makes sense to create this structure it if the SQL query ensures that spec_num and spec_fld are identical in all lines.

The solution for this special case is very simple:

JavaScript

If spec_num and spec_fld are not identical in all lines, you must first group by spec_num and then by spec_fld. The solution is then a 4 dimensional array.

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