PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Willy Svenningsson  >  PHP Group By Array  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Test
Class: PHP Group By Array
Group bidimensional arrays by a given element
Author: By
Last change:
Date: 6 years ago
Size: 3,662 bytes
 

Contents

Class file image Download
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
       
// This example results in
        // 1 Html table with some pretty hierarchy
        // 2 "pretty" array
        // 3 plain print_r of result
       
include "GroupBy.class.php";

       
$dataRows = [
                [
1, "Andy", "PHP", "4"],
                [
1, "Andy", "C#", "6"],
                [
2, "Josh", "C#", "6"],
                [
2, "Josh", "ASP", "4"],
                [
1, "Andy", "SQL", "6"],
                [
3, "Steve", "SQL", "2"],
                [
4, "self", "SQL", "30"],
                [
4, "self", "Smalltalk", "5"],
                [
4, "self", "C", "33"],
                [
4, "self", "Swedish", "65"]
        ];
       
// Group on the third column
       
$res = (new GroupBy())->groupBy($dataRows, [ 2 ]);

       
// Group on Years and Language
// $res = (new GroupBy())->groupBy($dataRows, [ 3, 2 ]);
        // You get an array back
        // Each row contains two arrays
        // First array: The key as an array of columns values
        // Second array: An array of all rows to this key

       
        // The above call [ 2 ] will result in
        // (pasted from output of this test)
       
$x = [
                [
                        [
"ASP"],
                        [
                                [
"2", "Josh", "4"]
                        ]
                ],
                [
                        [
"C"],
                        [
                                [
"4", "self", "33"]
                        ]
                ],
                [
                        [
"C#"],
                        [
                                [
"1", "Andy", "6"],
                                [
"2", "Josh", "6"]
                        ]
                ],
                [
                        [
"PHP"],
                        [
                                [
"1", "Andy", "4"]
                        ]
                ],
                [
                        [
"SQL"],
                        [
                                [
"1", "Andy", "6"],
                                [
"3", "Steve", "2"],
                                [
"4", "self", "30"]
                        ]
                ],
                [
                        [
"Smalltalk"],
                        [
                                [
"4", "self", "5"]
                        ]
                ],
                [
                        [
"Swedish"],
                        [
                                [
"4", "self", "65"]
                        ]
                ]
        ];
       
// Usage (dummy)
       
foreach ($res as $aGroup)
        {
           
$groupKey = $aGroup[0];
           
$groupRows = $aGroup[1];
            foreach (
$groupRows as $groupRow)
            {
               
// We got all columns in $groupRow
                // (And the key cols in $groupKey
           
}
        }
       
// Display as a HTML table

       
echo "<code><table border='1'>";
       
$runningLinenumber = 1;
        foreach (
$res as $aGroup)
        {
           
$groupKey = $aGroup[0];
           
$groupRows = $aGroup[1];
           
// first row. calc rowspan
           
echo "<tr>";
            echo
"<td>" . $runningLinenumber++ . "</td>"; // optional. but nice for user row interaction
           
echo "<td rowspan='" . count($groupRows) . "' valign=\"top\">" . implode(",", $groupKey) . "</td>";
            echo
"<td>" . implode("</td><td>", $groupRows[0]) . "</td>";
            echo
"</tr>";
           
// end first row
           
for ($r = 1; $r < count($groupRows); ++$r)
            {
               
$groupRow = $groupRows[$r];
                echo
"<tr>";
                echo
"<td>" . $runningLinenumber++ . "</td>"; // optional
               
echo "<td>" . implode("</td><td>", $groupRow) . "</td>";
                echo
"</tr>";
            }
            echo
"</tr>";
        }
        echo
"</table></code>";

// Display as php array initial (copy and paste from the output)

       
echo '<pre>';
        echo
"[\n";
       
$keyAndData = [];
        for (
$grpNr = 0; $grpNr < count($res); ++$grpNr)
        {
           
$groupKey = $res[$grpNr][0];
           
$keyTTY = '"' . implode('","', $groupKey) . '"';
            echo
" [\n";
            echo
" [" . $keyTTY . "],\n";
            echo
" [\n";
           
$groupRows = $res[$grpNr][1];
            for (
$rowNr = 0; $rowNr < count($groupRows); ++$rowNr)
            {
               
$groupRow = $groupRows[$rowNr];
               
$aRow = '"' . implode('","', $groupRow) . '"';
                echo
" [" . $aRow . "]";
                if (
$rowNr != count($groupRows) - 1)
                    echo
",\n";
                else
                    echo
"\n";
            }
            echo
" ]\n";
            echo
" ]";
            if (
$grpNr != count($res) - 1)
                echo
",\n";
            else
                echo
"\n";
        }
        echo
"]\n";
        echo
print_r($res, true);
        echo
'</pre>';
       
?>
</body>
</html>

For more information send a message to info at phpclasses dot org.