php - Can I use PDO::FETCH_GROUP to group results by two values -


I am using PHP and PDO to recover a set of values ​​from a database and then from the first column to the group Code with this: $ result = $ sth- & gt; Get All (PDO :: FETCH_GROUP | PDO :: FETCH_NUM); This means that if my data is in this format:

  | Column1. Column2. Column 3 | 1 | 2 | One | | 1 | 2 | B   

It comes back in this form:

  1: {[column2: 2, column3: a], [Column 2: 2, Column 3: B]}   

How do I group the columns by column 1 and column 2, so I get something:

  1: {2: { Column3: a, Column3: b}}   

Is it possible to do this with a combination of PDO constants?

Thank you

As you've seen, it's easy to easily arrange an array Grouping with code with PDO :: FETCH_GROUP can be easy. What needs to be done is to convert the 2-dimension array into a 3-dimensional array, but it is not possible to rebuild it this way.

You need this to manually do it manually through the nested loop, such as:

  // Assume that the result of $ is: $ Result = [[1, 2, 'a'], [1, 2, 'b']]; $ newResult = []; Foreign Currency ($ $ as result of $) {$ newResult [$ line [0]] [$ row [1]] [] = $ line [2]; } Var_dump ($ newResult);   

Here returns:

  array (1) {[1] = & gt; Array (1) {[2] = & gt; Array (2) {[0] = & gt; String (1) "a" [1] = & gt; String (1) "b"}}}   

And it looks like you.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -