fork download
  1. <?php
  2.  
  3. $data = array(
  4. "status" => "new",
  5. "type" => "type1",
  6. "source" => "source1",
  7. "other" => "other1",
  8. "count" => "1",
  9. ),
  10. "status" => "new",
  11. "type" => "type1",
  12. "source" => "source1",
  13. "other" => "other1",
  14. "count" => "2",
  15. ),
  16. "status" => "new",
  17. "type" => "type1",
  18. "source" => "source1",
  19. "other" => "other1",
  20. "count" => "5",
  21. ),
  22. "status" => "done",
  23. "type" => "type1",
  24. "source" => "source1",
  25. "other" => "other1",
  26. "count" => "1",
  27. ),
  28. "status" => "done",
  29. "type" => "type1",
  30. "source" => "source2",
  31. "other" => "other1",
  32. "count" => "3",
  33. ),
  34. "status" => "done",
  35. "type" => "type2",
  36. "source" => "source1",
  37. "other" => "other1",
  38. "count" => "1",
  39. ),
  40. "status" => "done",
  41. "type" => "type2",
  42. "source" => "source1",
  43. "other" => "other2",
  44. "count" => "5",
  45. ),
  46. "status" => "done",
  47. "type" => "type3",
  48. "source" => "source1",
  49. "other" => "other1",
  50. "count" => "1",
  51. ),
  52. "status" => "done",
  53. "type" => "type3",
  54. "source" => "source1",
  55. "other" => "other2",
  56. "count" => "5",
  57. ),
  58. );
  59.  
  60. $groups = array("status", "type", "source", "other");
  61.  
  62. function groupBy($data = array(), $groupBy = array())
  63. {
  64.  
  65. $seen = array();
  66. $result = array();
  67.  
  68. foreach($data as $node){
  69. if(!in_array($node[$groupBy[0]], $seen)) {
  70. // Insert first grouping level as checked level
  71. $seen[] = $node[$groupBy[0]];
  72.  
  73. // Setting starting parent path
  74. $path = array();
  75. $path[$groupBy[0]] = $node[$groupBy[0]];
  76.  
  77. // Get first group children
  78. $result[] = getChildren($node[$groupBy[0]], 0, $data, $groupBy, $path);
  79. }
  80. }
  81.  
  82. return $result;
  83. }
  84.  
  85. function getChildren($parent, $currentLevel, $data = array(), $groupBy, $path = array())
  86. {
  87. $nextLevel = $currentLevel + 1;
  88.  
  89. // Don't group deeper than needed and escape
  90. if($nextLevel > count($groupBy)){
  91. return array();
  92. }
  93.  
  94. $seen = array();
  95. $children = array();
  96. $dataSet = array();
  97.  
  98.  
  99. foreach($data as $node){
  100.  
  101. // Add next record to $path (to trace each record parent)
  102. if($groupBy[$nextLevel]) {
  103. $path[$groupBy[$nextLevel]] = $node[$groupBy[$nextLevel]];
  104. }
  105.  
  106. // Some magic, to check if we are on correct segment (trace the same as should be)
  107. $goInside = true;
  108. foreach($path as $fieldName => $fieldValue) {
  109. if($node[$fieldName] != $fieldValue) {
  110. $goInside = false;
  111. break;
  112. }
  113. }
  114.  
  115. // Just add data
  116. if($node[$groupBy[$currentLevel]] == $parent && $goInside) {
  117. $dataSet[] = $node;
  118. }
  119.  
  120. // TODO: I don't know why, but this is working
  121. if($node[$groupBy[$currentLevel]] == $parent && !in_array($node[$groupBy[$nextLevel]], $seen) && $goInside) {
  122. $seen[] = $node[$groupBy[$nextLevel]];
  123. $tempChildren = getChildren($node[$groupBy[$nextLevel]], $nextLevel, $data, $groupBy, $path);
  124. if($tempChildren && ($tempChildren["data"] || $tempChildren["children"])) {
  125. $children[] = $tempChildren;
  126. }
  127. }
  128. }
  129.  
  130. $result = array(
  131. 'fieldName' => $groupBy[$currentLevel],
  132. 'value' => $parent,
  133. );
  134.  
  135. if($children) {
  136. $result['children'] = $children;
  137. } else {
  138. if($dataSet) {
  139. $result['data'] = $dataSet;
  140. }
  141. }
  142.  
  143. return $result;
  144. }
  145.  
  146. echo "<pre>";
  147. print_r(groupBy($data, $groups));
  148. echo "</pre>";
Success #stdin #stdout #stderr 0s 82560KB
stdin
Standard input is empty
stdout
<pre>Array
(
    [0] => Array
        (
            [fieldName] => status
            [value] => new
            [children] => Array
                (
                    [0] => Array
                        (
                            [fieldName] => type
                            [value] => type1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [fieldName] => source
                                            [value] => source1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other1
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => new
                                                                            [type] => type1
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 1
                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [status] => new
                                                                            [type] => type1
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 2
                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [status] => new
                                                                            [type] => type1
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 5
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [fieldName] => status
            [value] => done
            [children] => Array
                (
                    [0] => Array
                        (
                            [fieldName] => type
                            [value] => type1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [fieldName] => source
                                            [value] => source1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other1
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type1
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 1
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [fieldName] => source
                                            [value] => source2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other1
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type1
                                                                            [source] => source2
                                                                            [other] => other1
                                                                            [count] => 3
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [fieldName] => type
                            [value] => type2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [fieldName] => source
                                            [value] => source1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other1
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type2
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 1
                                                                        )

                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other2
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type2
                                                                            [source] => source1
                                                                            [other] => other2
                                                                            [count] => 5
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [fieldName] => type
                            [value] => type3
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [fieldName] => source
                                            [value] => source1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other1
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type3
                                                                            [source] => source1
                                                                            [other] => other1
                                                                            [count] => 1
                                                                        )

                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [fieldName] => other
                                                            [value] => other2
                                                            [data] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [status] => done
                                                                            [type] => type3
                                                                            [source] => source1
                                                                            [other] => other2
                                                                            [count] => 5
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
</pre>
stderr
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 111
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 130
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 131
PHP Notice:  Undefined offset: 4 in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index:  in /home/msHZRL/prog.php on line 132
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133
PHP Notice:  Undefined index: data in /home/msHZRL/prog.php on line 133