public function groupBy($array, $columns)
    {
        // ensure $columns parameter is array
        $columns = (array) $columns;

        // get first group-by column
        $col = array_shift($columns);

        $result = array();
        foreach ($array as $row) {
            $key = (string) is_object($row) ? $row->$col : $row[$col];
            if (!isset($result[$key])) {
                $result[$key] = array();
            }

            $result[$key][] = $row;
        }

        // do subsequent group by calls
        if (count($columns)) {
            foreach (array_keys($result) as $key) {
                $result[$key] = $this->groupBy($result[$key], $columns);
            }
        }

        return $result;
    }
public function groupBy($array, $columns)
    {
        // ensure $columns parameter is array
        $columns = (array) $columns;

        // get first group-by column
        $col = array_shift($columns);

        $result = array();
        foreach ($array as $row) {
            $key = (string) is_object($row) ? $row->$col : $row[$col];
            if (!isset($result[$key])) {
                $result[$key] = array();
            }

            $result[$key][] = $row;
        }

        // do subsequent group by calls
        if (count($columns)) {
            foreach (array_keys($result) as $key) {
                $result[$key] = $this->groupBy($result[$key], $columns);
            }
        }

        return $result;
    }
public function groupBy ( $ array , $ columns ) { // ensure $columns parameter is array $ columns = ( array ) $ columns ; // get first group-by column $ col = array_shift ( $ columns ) ; $ result = array ( ) ; foreach ( $ array as $ row ) { $ key = ( string ) is_object ( $ row ) ? $ row -> $ col : $ row [ $ col ] ; if ( ! isset ( $ result [ $ key ] ) ) { $ result [ $ key ] = array ( ) ; } $ result [ $ key ] [ ] = $ row ; } // do subsequent group by calls if ( count ( $ columns ) ) { foreach ( array_keys ( $ result ) as $ key ) { $ result [ $ key ] = $ this -> groupBy ( $ result [ $ key ] , $ columns ) ; } } return $ result ; }
Group a 2-dim array by a set of columns.

@param array            $array   2-dim array
@param int|string|array $columns index of the group-by column(s)

@return array
Group a 2 - dim array by a set of columns .