Search code examples
phpmysqlgrouping

Group mysql rows in an array by column value


I have a mysql table in this format

Mysql table format
_______________________________
| column1 | column2 | column3 |
|_________|_________|_________|
|   A     |  val Z  |  val Y  |
|_________|_________|_________|
|   A     |  val X  |  val W  |
|_________|_________|_________|
|   A     |  val V  |  val U  |
|_________|_________|_________|
|   B     |  val T  |  val S  |
|_________|_________|_________|
|   B     |  val R  |  val Q  |
|_________|_________|_________|
|   C     |  val P  |  val O  |
|_________|_________|_________|

What I need to do is return all rows of an occurrence of each value in column1 in an array. The array may be something like this:

$A = array($row1,$row2,$row3);

$row1 = array("column1"=>"A", "column2"=>"val Z", "column3"=>"val Y",);

$row2 = array("column1"=>"A", "column2"=>"val X", "column3"=>"val W",);

$row3 = array("column1"=>"A", "column2"=>"val V", "column3"=>"val V",);

I hope I am clear enough. Basically I need rows to be grouped by uniqueness of values in column1.

The mysql result should contain all the rows in an array grouped as said above. something like this:

$result = array($A, $B, $C);

What I need to do with the output is display it in a table. If there is a better way of doing it please let me know. I would like the table to take this format:

_________________________________________________
|  Header1  |  Header2  |  Header3  |  Header4  |
|___________|___________|___________|___________|
|    A      |   val Y   |   val W   |   val U   |
|___________|___________|___________|___________|
|    B      |   val S   |   val Q   |           |
|___________|___________|___________|___________|
|    C      |   val O   |           |           |
|___________|___________|___________|___________|

This is the only way I can only think of doing it. If there is a better way of doing it please let me know. Thank you for helping.


Solution

  • Assuming the fact that you want to display only the column3 data in the table, the solution would be like this:

    Change your query from

    SELECT * FROM table;
    

    to

    SELECT column1, GROUP_CONCAT(column3 SEPARATOR ',') as column3 
    FROM table 
    GROUP BY column1 
    ORDER BY COUNT(column1) DESC
    

    Execute the query and follow the below steps,

    1. Construct an associative array from the result set.
    2. Extract the keys from the associative array.
    3. Count the number of columns in the table, and finally
    4. Construct the table

    Here's the code:

    $sql = "SELECT column1, GROUP_CONCAT(column3 SEPARATOR ',') as column3 FROM table_name GROUP BY column1 ORDER BY COUNT(column1) DESC";
    $result_set = mysqli_query($conn, $sql);
    
    // construct an associative array
    $result_array = array();
    while($row = mysqli_fetch_assoc($result_set)){
        $result_array[$row['column1']] = explode(",", $row['column3']);
    }
    
    // extract the keys from the associative array
    $result = array_keys($result_array);
    
    // count the number of columns in the table
    $len = count($result_array[$result[0]]);
    
    // construct the table
    echo '<table border="1">';
    
    // display table headings 
    echo '<tr>';
    for($i = 1; $i <= $len + 1; ++$i){
        echo '<th>Header' . $i . '</th>';
    }
    echo '</tr>';
    
    // display table contents
    foreach($result as $k){
        echo '<tr>';
        echo '<td>' . $k . '</td>';
        for($i = 0; $i < $len; ++$i){
            echo '<td>';
            echo isset($result_array[$k][$i]) ? $result_array[$k][$i] : '';
            echo '</td>';
        }
        echo '</tr>';
    }
    
    echo '</table>';
    

    Output:

    enter image description here


    Note: If you want to see the structure of $result_array array, do var_dump($result_array);